-2

Thanks for your help earlier.. Here I am with a new problem facing the same output. scanf() is not working. I am using MACROs in it and this time format is correct LOL.. please have a look and tell me what am i doing wrong here. i am trying to take two characters as input and test whether they are "uppercase or not" or "lowercase or not". my program scans for ch1 but doesn't scan for ch2; i tried flushing the input using "fflush(stdin);" but still the same. when i printed the value of ch2 to see what it is taking in account it shows "10" that's where i tried to flush the input but still the same output. so please have a look and please tell me my mistake. I'll be very thankful.

       #include <stdio.h>
       #define UPPERCASE(x) {\
                             if(x>=65 && x<=90)\
                               printf("Uppercase letter\n");\
                             else printf("not Uppercase\n");}
       #define LOWERCASE(x) {\
                             if(x>=97 && x<=122)\
                               printf("LOWERCASE LETTER\n");\
                             else printf("not lowercase\n");}
       #define BIGGER(x,y) { \
                             if(x>y)\
                               printf("%d is biger\n",x);\
                             else printf("%d is bigger\n",y);}


       int main()
       {
            char ch1,ch2;
            int x,y;

            printf("enter a UPPERCASE LETTER\n");
            scanf("%c",&ch1);
            UPPERCASE(ch1);

            printf("enter a LOWERCASE LETTER \n");
            fflush(stdin);
            scanf("%c",&ch2);
            LOWERCASE(ch2);


            printf("enter  two numbers\n");
            scanf("%d%d",&x,&y);
            BIGGER(x,y);
            return 0;
          }

Here my output

Ralf Stubner
  • 24,387
  • 3
  • 31
  • 63
  • 3
    For starters, [stop doing that: `fflush(stdin);`](https://stackoverflow.com/questions/2979209/using-fflushstdin). It invokes *undefined behavior*. You need to consume the newline from the input stream, one way or another. `" %c"` (note the leading space, useful for single-character reads) is a common way to do that after a previous formatted input request. [See this question](https://stackoverflow.com/questions/46166734/why-does-scanf-not-work-when-access-a-input-of-char-type-in-c). – WhozCraig Oct 01 '18 at 08:35
  • yes it worked. but i still don't get it. please elaborate. why as you say a leading space or consuming a new line is necessary? – Austin Spark Oct 01 '18 at 08:43
  • Did you *read* the linked question in my comment? – WhozCraig Oct 01 '18 at 08:43
  • yes, but still unclear. isn't it that scanf() just suppose to take a input. what a blank has to do in it? – Austin Spark Oct 01 '18 at 08:46
  • [Try this one](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – WhozCraig Oct 01 '18 at 08:59
  • FYI there exist methods of seeing if a character is uppercase - isupper() - and all manner of other similar checks – Chris Turner Oct 01 '18 at 09:12
  • i know. but this program is not about to check the case of an alphabet, its about the use of MACROs. thanks @WhozCraig i got it now. I appreciate your help guys. – Austin Spark Oct 04 '18 at 07:42

1 Answers1

1

You can use flushall() function that clears all buffers associated with input streams, and writes any buffers associated with output streams.

Flushall() isn't C, but a vendor specific extension.

OR

Other alternative is to use space before %c


Example

char ch;
scanf(" %c", &ch);

The other problem is with the use of macro. Inside macro definition you are using same name as local variable x and y of main()

Zahid Khan
  • 750
  • 1
  • 7
  • 14
  • okay that space before %c worked but using same name in MACROs does really affect? bcos that part of the program worked fine. what i don't get is..why a space before %c is necessary ? – Austin Spark Oct 01 '18 at 08:51