I need to write a program that randomly generates multiplication problems based on how many the user says they want. So far this is what i've got:
Code:
#include <stdio.h>
#include <time.h>
int main () {
int number, initialnumber, first, second, guess, answer;
//Seed random number
srand(time(0));
printf("How many problems do you want?\n");
scanf("%d", &initialnumber);
number = initialnumber;
int start = time(0);
while (number > 0) {
//Create problem numbers
first = 1 + rand()%12;
second = 1 + rand()%12;
answer = first * second;
printf("Answer: %d x %d\n", first, second);
scanf("%d", &guess);
if (guess == answer)
number --;
else
printf("Incorrect, try again.\nAnswer: %d x %d\n", first, second);
number --;
}
int end = time(0);
int timespent = end - start;
printf("You completed %d problems in %d seconds.\n", initialnumber, timespent);
return 0;
}
The problem I have is if they get it wrong it's supposed to display the same question again. I know that the else statement is completely useless as it doesn't even scan in their answer, I just put it there when I was brainstorming. How can I get it to check the new answer they've given instead of looping back around? Do I need to do something other than a while loop?