C - Getting double output?

Reply

Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

C - Getting double output?

 
0
  #1
Feb 28th, 2006
I must be missing something here. I am having trouble when I try to get an input. The numbers work fine, but once you get the correct answer, the area:

  1. void tryanother() {
  2.  
  3. char yorn;
  4.  
  5. printf("Would you like to try another one? (y/n)\n");
  6. scanf("%c", &yorn);
  7.  
  8. if (yorn == 'y')
  9. getnum();
  10. else if (yorn == 'n')
  11. return;
  12. else if (yorn != 'y' || yorn != 'n') {
  13. printf("Invalid choice\n");
  14. tryanother();
  15. }
  16.  
  17. }

isn't right. It will print "Would you like to try another (y/n)." "Invalid Choice." "Would you like to try another (y/n)" then wait for your input... why is it doing this?

Thank you.

Here is the rest of the code:



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. void getnum ();
  7. void tryagain(int, int, int);
  8. void tryanother();
  9.  
  10. void getnum() {
  11.  
  12. int num1, num2, solution;
  13. num1 = (rand() % 9);
  14. num2 = (rand() % 9);
  15.  
  16. solution = num1 * num2;
  17.  
  18. tryagain (num1, num2, solution);
  19. }
  20.  
  21.  
  22. void tryagain(int num1, int num2, int solution) {
  23.  
  24. int guess;
  25.  
  26. printf("How much is %d times %d ?\n", num1, num2);
  27. scanf("%d", &guess);
  28.  
  29. if(guess == solution) {
  30. printf("Very Good!\n\n");
  31. tryanother();
  32. }
  33. else
  34. tryagain(num1, num2, solution);
  35. }
  36.  
  37. void tryanother() {
  38.  
  39. char yorn;
  40.  
  41. printf("Would you like to try another one? (y/n)\n");
  42. scanf("%c", &yorn);
  43.  
  44. if (yorn == 'y')
  45. getnum();
  46. else if (yorn == 'n')
  47. return;
  48. else if (yorn != 'y' || yorn != 'n') {
  49. printf("Invalid choice\n");
  50. tryanother();
  51. }
  52.  
  53. }
  54.  
  55.  
  56. int main() {
  57.  
  58. srand(time (NULL));
  59.  
  60. getnum();
  61.  
  62. return 0;
  63.  
  64. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C - Getting double output?

 
0
  #2
Feb 28th, 2006
when you press 'y' followed by <Enter> key, there are two keys in the keyboard buffer -- so the second time through the code it is probably getting the '\n' key. you need to flush the keyboard buffer to remove the '\n'.

  1. scanf("%c", &yorn);
  2. getchar(); //flush the keyboard buffer of the '\n' key

or you could use fgets() which will flush the '\n' too assuming the user types on 'y' followed by <Enter>. If anything else is typed then those keys will also be in the keyboard buffer. There is no easy standard solution to the problem. Make the buffer in the code below big enough and fgets() will probably be able to fill it with all the keys in the buffer.
  1. char buf[3];
  2. fgets(buf,sizeof(buf),stdin);
  3. yorn = buf[0];
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: C - Getting double output?

 
0
  #3
Feb 28th, 2006
Alright, I throw the gatchar(); in there, but then if I hit y or n it gives me invalid choice and there is no exiting the loop...
??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C - Getting double output?

 
0
  #4
Feb 28th, 2006
use getchar() after each numeric input using scanf(). you need it in two places in your program.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 105
Reputation: jhdobbins is an unknown quantity at this point 
Solved Threads: 3
jhdobbins jhdobbins is offline Offline
Junior Poster

Re: C - Getting double output?

 
0
  #5
Feb 28th, 2006
ty much dragon

it is solved!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C - Getting double output?

 
0
  #6
Feb 28th, 2006
With fgets(), you risk not flushing the entire buffer, and with a single getchar() you have the same problem. The best solution is this:
  1. int c;
  2. while((c = getchar()) != '\n' && c != EOF);
c has to be an int because EOF is an int value.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C - Getting double output?

 
0
  #7
Feb 28th, 2006
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 39
Reputation: comwizz is an unknown quantity at this point 
Solved Threads: 0
comwizz's Avatar
comwizz comwizz is offline Offline
Light Poster

Re: C - Getting double output?

 
0
  #8
Mar 1st, 2006
I did not understand that if the size of the buf variable is 3 then after getting input "y" and "\n" how does stdin terminate the coming input i.e. what would be buf[2]??
  1. char buf[3];
  2. fgets(buf,sizeof(buf),stdin);
  3. yorn = buf[0];
Also , I did not understand why
  1. while((c = getchar()) != '\n' && c != EOF);
this is required. I have gone through faq recommended by dwks but still I am not clear . Please help,
Thanks,
comwizz.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C - Getting double output?

 
0
  #9
Mar 1st, 2006
lets say that instead of typing just y + '\n', you type a whole bunch of keys then '\n' at the end. fgets() will only grab from the keyboard buffer the number of keys that will fit in the buffer (sizeof the buffer - 1 for null terminator), the remaining keys will stay in the keyboard buffer until your program removes them. And the buffer returned by fgets() will contain '\n' only if there is one in the input stream.

The loop that dwks posted is probably the savest way to flush out (empty) the keyboard buffer that contains an unknown number of characters.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC