Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getchar () vs scanf

Status
Not open for further replies.

jdeejay

Programmer
Oct 5, 2001
44
US
I have two sets of prompts. The user picks from 3 choices in the first prompt, the code executes and then prompt number two appears. Now when I read in the second prompt (the first time) it is invalid. when I read a second time, it is valid. Then it alternates back and forth--invalid then valid. What is the best way to read these two prompts without holding onto any newlines or CR's. I have every combination of scanf and getchar that I can think of and also using fflush(NULL); in between. All with less than desirable results.

What should I be using?

Thanks in advance,
J
 
try fflush(stdin);........you dont say what the prompts ask for int or char value. there are a number of different ways to read charcters, some of which are compiler and os dependant. have a look at fgetc(stdin); Hoping to get certified..in C programming.
 
I think you are reading the choice as a single character. If so, read the single character using scanf() with %s options. See the folloing code.

char c;
scanf("%1s", &c);

This will read a single character perfectly without any \n and \r problems.

Regards,
Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top