Tests on characters

I'm trying to use getchar to determine what to do in the program.

So if I type in p in the program when it asks, I want it to run a certain statement block. Example below.

1
2
3
4
5
6
7
8
in = getchar();

if ( in == 'p')
{
 TEST blah blah;

}


However, when I type in p, the test is false - which it shouldn't.

Any help guys? (I'm writing in C )
Last edited on
That code should work.
Can you post your actual code?
Well, you should post a little more code for me to properly understand.

But the reasons for the input to be wrong could be that there are other characters in the input stream waiting to be read and getchar reads that instead of your 'p'.

To avoid that use fflush(stdin); right before your getchar() call. I've had those problems with getchar before.

Another thing could be that you are entering a capital P instead. So your statement in line 3 would, of course, equate to false because you are comparing it with a lower case 'p'.

Always do something like this to avoid case problems:char ch = tolower(getchar());

Other than the above mentioned possibilities, I can't think of anything else. Get back to me if your problem persists and post a little more code - so that we can analyze the execution properly.

If my English seems bad, please forgive me.

EDIT: Another thing, you could check if your getchar call is failing.
Like,
1
2
3
4
5
6
7
if((ch=getchar()) == EOF)
{
 /* getchar() failed for some reason...
  * print error message or debug
  */
   perror("getchar failed.");
} 
Last edited on
yah it should, unless it's unfortunately grabbing the whole string with the null.
@brokenbot

getchar() functions isn't used for reading strings. It reads one single character from the input buffer, even the newline, and returns it. It will only return 0 (NULL)when it reads 0 from the stdin, EOF (usually -1) in case an error occured.

We can use errno for debugging purposes if it happens to fail. I've seen a few documents stating that it does set errno. Never tried it myself though.
Last edited on
So if it returns \n also, then the test should be (in = 'p\n') ?
No No.

Please, I've said it in my previous post, it returns a single character; not a string. There is no such character as 'p\n', it is incorrect. It will return plain '\n' if it reads one...

Suppose you use other input functions such as scanf. Let's say you call scanf("%d");.
Now, lets assume you type 26 and press enter. You will also without knowing enter a '\n' in the input buffer; scanf reads the 26 and ignores the '\n'.
It is safe if you call scanf again as it tends to ignore it.
But, if you call getchar, it will most likely read that '\n' and you won't get a chance to enter anything.
You should use fflush(stdin) to flush out all the garbage characters, if any.

However, it may not be your problem. That is why I asked for you to post your full code.
And please read my first post.

unoriginal wrote:
Always do something like this to avoid case problems:char ch = tolower(getchar());

EDIT: Another thing, you could check if your getchar call is failing.
Like,
1
2
3
4
5
6
7
if((ch=getchar()) == EOF)
{
 /* getchar() failed for some reason...
  * print error message or debug
  */
   perror("getchar failed.");
} 



If none of what I suggested worked then please post the complete code you think is creating the problem. I and others can't help much with the little info you just posted.

-unoriginal (if my English seems bad, please forgive me.)
Last edited on
Topic archived. No new replies allowed.