getchar() not working (fixed)

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
This program isn't working, it doesn't get the character, just exits.

________________________________________________________________________________________
//Program Description: The program chooses a random integer to be guessed by the user.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int getrandomint();

main()
{
-- - - - - - int number; - - - - - // random number
-- - - - - - int userin; - - - // number input by user

-- - - - - - srand(time(NULL));

-- - - - - - printf("I have a number between 1 and 1000.\n");
-- - - - - - printf("Can you guess my number?\n");

-- - - - - - do - - - - - - - // decides, at the end, wether to loop back or not
-- - - - - - {

-- - - - - - - - - - - - - number = getrandomint();

-- - - - - - - - - - - - - do - - - - - // loops until the user guesses the correct number
-- - - - - - - - - - - - - {
-- - - - - - - - - - - - - - - - - - - printf("\nEnter guess: ");
-- - - - - - - - - - - - - - - - - - - scanf("%d", &amp;userin);


-- - - - - - - - - - - - - - - - - - - if(userin < number) - // compares random number and number input by user
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo low. Try again.");

-- - - - - - - - - - - - - - - - - - - else if(userin > number)
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nToo high. Try again.");

-- - - - - - - - - - - - - - - - - - - else
-- - - - - - - - - - - - - - - - - - - - - - - - - printf("\nExcellent! You guessed the number!\n");
-- - - - - - - - - - - - - }
-- - - - - - - - - - - - - while(userin != number);

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');
}

int getrandomint()
{
-- - - - - - int x;

-- - - - - - x = 1 + rand() % 1000;
-- - - - - - return x;
}
________________________________________________________________________________________

It does get the character however you you change the printf before getchar to this:

printf("\nWould you like to try again (press y or n)? %c", getchar());

Anyone know what the problem could be?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You can't pass getchar() as an argument to a function. It is implemented as a macro. You could use fgetc(FILE * stream) instead (same man page). Also, I don't think you want to be getting the character before printing out the "Would you..." sentence. That'll give them a blank line and print the sentence after they hit enter.

I hope that helps, I didn't really understand the orginal question (this part):
It does get the character however you you change the printf before getchar to this:
Also, you might want to think about not using scanf for a number. Have you tried entering a non-numeric character at that point?


Edit: getchar() could be impemented as a macro. I guess it depends on the library you're using. The first man page I read was on an HP-UX system and implied that it was. The second was on a Redhat system which implied that it might. Anyways, try fgetc(stdin)...
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
That's a weird way to write the code. The printf shouldn't print anything until after you enter a character.

------------------------- printf("\nWould you like to try again (press y or n)? %c", getchar());
------------ }
------------ while(getchar() == 'y');
You realize the user has to enter junk once, then y for it to actually loop, right? Instead, create a char foo, do foo=getchar(), and then check foo.
 

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
That's a weird way to write the code. The printf shouldn't print anything until after you enter a character.

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');
You realize the user has to enter junk once, then y for it to actually loop, right? Instead, create a char foo, do foo=getchar(), and then check foo.
I don't exactly understand what 'foo' is.

What I think you are saying is that you want me to modify it to this:

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? ");
-- - - - - - - - - - - - - variable = getchar();
-- - - - - - }
-- - - - - - while(variable == 'y');

You realize the user has to enter junk once, then y for it to actually loop, right?
Logically, it would make sense that it would occur that way, but if I remove one of the [getchar()]s. I'm not able to input anything. The program just exits.

Doin it the way I have it, I only have to input the char once.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
Originally posted by: VIAN
That's a weird way to write the code. The printf shouldn't print anything until after you enter a character.

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');
You realize the user has to enter junk once, then y for it to actually loop, right? Instead, create a char foo, do foo=getchar(), and then check foo.
I don't exactly understand what 'foo' is.

What I think you are saying is that you want me to modify it to this:

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? ");
-- - - - - - - - - - - - - variable = getchar();
-- - - - - - }
-- - - - - - while(variable == 'y');

You realize the user has to enter junk once, then y for it to actually loop, right?
Logically, it would make sense that it would occur that way, but if I remove one of the [getchar()]s. I'm not able to input anything. The program just exits.

Doin it the way I have it, I only have to input the char once.

he just used foo as the variable name

and yes, what you ssaid is right, do the getchar once and then check it

Do you understand why your original way wasn't a good approach?
 

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
No you guys don't get it.


My original way is the way you suggested with the variable. But that didn't work, so I had to modify it to this new way.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
this is what i would do (i hate do while loops for some reason)

i would just use a while loop

before entering the while loop set some character = 'y'

 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
Originally posted by: VIAN
No you guys don't get it.


My original way is the way you suggested with the variable. But that didn't work, so I had to modify it to this new way.

-- - - - - - - - - - - - - printf("\nWould you like to try again (press y or n)? %c", getchar());
-- - - - - - }
-- - - - - - while(getchar() == 'y');

Please show me where the variable is. You're calling getchar() twice. Ignoring any other issues with the code, this is *wrong*. Look at what cchen wrote.
 

lchyi

Senior member
May 1, 2003
935
0
0
Wow, looks like someone's doing a programming assignment... have fun! (And go to office hours!) Haha just kidding.

Okay, I should be helpful. Instead of just letting the program exit, did you put a pseudo debug statement in there? printf ("userin equals '%c' right now", userin)? Then you can see what it getchar grabbed and what it didn't.

But I think the replies actually have a good grasp of what you're doing, we do get it so don't say we don't. I haven't programmed in a while, but I'm pretty sure you need to store whatever you got from stdin using getChar() and compare that to 'y' not getChar() the function itself.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
The other thing to consider is that getchar will only return after the user has hit <enter> (I think?) The first call will get the first char entered but the second call will grab the newline character that was entered last time without actually giving the user a chance to give more input. If you're doing this in windows the next two calls will return useless data (*nix just one).
 

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
What you guys didn't get was that I've already tried the suggestions mentioned.

I consulted another teacher in my school.

getchar() gets a char in the keyboard buffer. You can use this statement alone, but after a previous scanf, it gets tricky. After pressing enter to the previous scanf, the last char in the keyboard will be the enter char.

That's why it wasn't working and required me to get the char twice.

He suggested I do the following before scanning in a char.

fflush(stdin)

This clears the buffer so that it will actually scan what I put in.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: VIAN
What you guys didn't get was that I've already tried the suggestions mentioned.

I consulted another teacher in my school.

getchar() gets a char in the keyboard buffer. You can use this statement alone, but after a previous scanf, it gets tricky. After pressing enter to the previous scanf, the last char in the keyboard will be the enter char.

That's why it wasn't working and required me to get the char twice.

He suggested I do the following before scanning in a char.

fflush(stdin)

This clears the buffer so that it will actually scan what I put in.

Did I not just point that out, more or less?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Yes, I am King of C!!! :p

Actually not. I'm putting myself through the mess that is C programming again for my compilers class (first battle with C in a few years) and I'm not liking it so far. Coming back from Java and Delphi is just so painful.