944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5495
  • C RSS
Feb 28th, 2006
0

C - Getting double output?

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Feb 28th, 2006
0

Re: C - Getting double output?

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];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 28th, 2006
0

Re: C - Getting double output?

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...
??
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Feb 28th, 2006
0

Re: C - Getting double output?

use getchar() after each numeric input using scanf(). you need it in two places in your program.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 28th, 2006
0

Re: C - Getting double output?

ty much dragon

it is solved!
Reputation Points: 10
Solved Threads: 3
Junior Poster
jhdobbins is offline Offline
105 posts
since Apr 2005
Feb 28th, 2006
0

Re: C - Getting double output?

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.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Feb 28th, 2006
0

Re: C - Getting double output?

Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Mar 1st, 2006
0

Re: C - Getting double output?

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
comwizz is offline Offline
39 posts
since Nov 2005
Mar 1st, 2006
0

Re: C - Getting double output?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 2d array
Next Thread in C Forum Timeline: execvp return help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC