Hello, I am trying to code a basic program which takes 2 inputs from a user based on a menu and then converts the second input to the menu choice chosen. For example, if 'a' is chosen and the number '5' was inputted then there would be a print stating that 5 is a digit.

Here is the code:

Code:
```bool isDigit(char num)
{
if(num>='0' && num<='9')
{
return true;
}
}

int main (void)
{
char user1 = 0; // declares a character variable for the user inputted function
char ch; // delcares a character variable for the user inputted character

printf ("*****************************************" // Displays the menu system
"\n* Character Arithmetic      *" // the title
"\n*****************************************"
"\n* Program instructions here *" // short instructions
"\n* Program instructions here *" // short instructions
"\n*****************************************"
"\n* Functions: *" // displays the 5 functions
"\n* a. isDigit *"
"\n* b. isLetter *"
"\n* c. toUpper *"
"\n* d. toLower *"
"\n* q. Quit *"
"\n*****************************************");

printf (" Choose a function: "); // prompts the user to enter a function
scanf ("%c", &user1); // saves the user input as 'user1'
printf ("Enter a character you would like to use the function on: \n"); // asks the user to input a character
scanf ("%c", &ch); // saves the user inputted character as 'ch'

if ( user1 == 'a' && isDigit(ch) )
{
printf ("%c is a digit!\n", ch);
}
}```

the problenm occurs when the it is supposed to ask me for my second input (ch) but it seems to just ask me for my input but not scan for it and assign it to ch instead it just exists! I can't seem to find anything wrong.

2. scanf() has a problem with %c. Since you are expecting just a char, and the newline left behind by scanf() always, is a char, it will pull off one newline char '\n', from the keyboard buffer, and seem to "skip" the input completely.

In reality, it didn't skip anything. It took the newline, thought that was what you wanted, and continued.

Add a getchar() after each scanf(), to pull the newlines off the keyboard buffer.

#1 complaint by C learners. A space between the " %, may do the same thing.

And Welcome to the Forum, SimarGill!

3. Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.

4. Originally Posted by SimarGill
Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.

scanf() does assigns a char/int/float to appropriate variables and keeps the enter key unread in the keyboard buffer.
getchar() will read the enter key from the buffer thinking the user has entered the enter key .

5. Originally Posted by SimarGill
Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.
When you made an entry, how many keys did you press?

You pressed 2 keys... the character and the enter key...
scanf() consumed the character you were asking for.
The enter key (\n) remains in the keyboard buffer.
Along comes another scanf() that's only looking for 1 character.
It finds the enter key and keeps on trucking.

When you add the space as in scanf(" %c"... you are telling it to ignore invisible characters and wait for the next keypress... which will, no surprises, put 2 more characters in the buffer.

I (personally) view this as a nasty flaw in C's input routines... but it is what it is and we get to live with it.

6. Originally Posted by CommonTater

I (personally) view this as a nasty flaw in C's input routines... but it is what it is and we get to live with it.
LOL

7. Originally Posted by rajarshi
LOL
OK... why do you think that's funny?

I've done a lot of programming in my days, now about half and half Pascal and C and I can tell you C's console input functions massively suck. As in, it's a real problem, just look how many times we get asked bout this each week.

8. Originally Posted by CommonTater
OK... why do you think that's funny?

I've done a lot of programming in my days, now about half and half Pascal and C and I can tell you C's console input functions massively suck. As in, it's a real problem, just look how many times we get asked bout this each week.
funny cuz...we got to live with it..and we can do absolutely nothing about it...
another flaw which I feel is (personal opinion)...operation between an integer and real always yields a real result...(no matter what we do..the integer is always promoted to real and we get a real result)

9. LOL ok i got it now, so im basically telling scanf to wait for 2 characters in the buffer since the enter key seems to skip it. Wow, that is a flaw.

10. Originally Posted by SimarGill
LOL ok i got it now, so im basically telling scanf to wait for 2 characters in the buffer since the enter key seems to skip it. Wow, that is a flaw.
Nope, just the opposite... %c tells scanf() to wait for 1 character, but you are entering 2... the character itself and the enter key both end up in the buffer.

11. Ah I got it now. Thanks but in that case, %c can be %d or whatever since it will always be waiting for one sort of input. Or is this issue only with %c. I never had this before.

12. Originally Posted by SimarGill
Ah I got it now. Thanks but in that case, %c can be %d or whatever since it will always be waiting for one sort of input. Or is this issue only with %c. I never had this before.
If you use %d scanf() will try to write sizeof(int) bytes... which may not be what you want.

It's an issue with %c and %s ... Look up scanf() in your compiler's library documentation (if you don't have it, get it) it should explain all this for you.

Trust me, you will keep banging into this over and over again.

13. Ohh alright I got it. In my book, it just that says that scanf reads an input from stdin stream. The string pointed to by format specifies the format of the items to be read. But I understand now, thanks to everyone!!

14. Originally Posted by SimarGill
Ohh alright I got it. In my book, it just that says that scanf reads an input from stdin stream. The string pointed to by format specifies the format of the items to be read. But I understand now, thanks to everyone!!
Seriously... get the library documentation specific to the compiler you are using... You will need to look these things up for yourself (which I do probably a hundred times a day) AND you will discover that your text book contains about 1/10th of the information you actually need.

15. Yes, I have downloaded it from my compilers website. And damn it, I spent \$150 for that book.