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;
    
}

Recommended Answers

All 8 Replies

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

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

char buf[3];
fgets(buf,sizeof(buf),stdin);
yorn = buf[0];
commented: solved problems with no trouble :) +1

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

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

ty much dragon :)

it is solved!

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.