954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C - Getting double output?

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:

void tryanother() {
     
     char yorn;
     
     printf("Would you like to try another one?  (y/n)\n");
     scanf("%c", &yorn);
    
     if (yorn == 'y')
        getnum();
     else if (yorn == 'n')
        return;
     else if (yorn != 'y' || yorn != 'n') {
        printf("Invalid choice\n");
        tryanother();
        }
     
}

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:

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


void getnum ();
void tryagain(int, int, int);
void tryanother();

void getnum() {
    
    int num1, num2, solution;
    num1 = (rand() % 9);
    num2 = (rand() % 9);
    
    solution = num1 * num2;
    
    tryagain (num1, num2, solution);
}


void tryagain(int num1, int num2, int solution) {
    
    int guess;
    
    printf("How much is %d times %d ?\n", num1, num2);
    scanf("%d", &guess);
    
    if(guess == solution) {
        printf("Very Good!\n\n");
        tryanother();
        }
    else
        tryagain(num1, num2, solution);
}

void tryanother() {
     
     char yorn;
     
     printf("Would you like to try another one?  (y/n)\n");
     scanf("%c", &yorn);
    
     if (yorn == 'y')
        getnum();
     else if (yorn == 'n')
        return;
     else if (yorn != 'y' || yorn != 'n') {
        printf("Invalid choice\n");
        tryanother();
        }
     
}        
 

int main() {
    
    srand(time (NULL));
    
    getnum();
    
    return 0;
    
}
jhdobbins
Junior Poster
105 posts since Apr 2005
Reputation Points: 10
Solved Threads: 3
 

when you press 'y' followed by 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'.

scanf("%c", &yorn);
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 . 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.

char buf[3];
fgets(buf,sizeof(buf),stdin);
yorn = buf[0];
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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...
??

jhdobbins
Junior Poster
105 posts since Apr 2005
Reputation Points: 10
Solved Threads: 3
 

use getchar() after each numeric input using scanf(). you need it in two places in your program.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ty much dragon :)

it is solved!

jhdobbins
Junior Poster
105 posts since Apr 2005
Reputation Points: 10
Solved Threads: 3
 

With fgets(), you risk not flushing the entire buffer, and with a single getchar() you have the same problem. The best solution is this:

int c;
while((c = getchar()) != '\n' && c != EOF);

c has to be an int because EOF is an int value.

dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 
dwks
Posting Whiz in Training
269 posts since Nov 2005
Reputation Points: 185
Solved Threads: 28
 

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]??

char buf[3];
fgets(buf,sizeof(buf),stdin);
yorn = buf[0];

Also , I did not understand why

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.

comwizz
Light Poster
39 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You