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