I was making some changes to my code so it'll randomly give either addition, subtraction, multiplication, division(before it would cycle through each one). But for some reason, previously working variables (question, wrongAnswers, totalQuestions, correctAnswers) are now coming up as undefined. What's causing this and how can I fix it?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int addition(void){
int number1, number2, addAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
addAnswer = (number1 + number2);
printf("%d + %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == addAnswer){
return 1;
}
else{
return 0;
}
}
int subtraction(void){
int number1, number2, subAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
subAnswer = (number1 - number2);
printf("%d - %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == subAnswer){
return 1;
}
else{
return 0;
}
}
int multiplication(void){
int number1, number2, multAnswer, response;
srand(time(NULL));
number1 = rand() % 12;
number2 = rand() % 12;
multAnswer = (number1 * number2);
printf("%d * %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == multAnswer){
return 1;
}
else{
return 0;
}
}
int division(void){
int number1, number2, divAnswer, response;
srand(time(NULL));
number1 = rand() % 12 + 1;
number2 = rand() % 12 + 1;
divAnswer = (number1 / number2);
printf("%d / %d = ?\n", number1, number2);
scanf("%d", &response);
if (response == divAnswer){
return 1;
}
else{
return 0;
}
}
int main(void) {
int response, int question = 0, correctAnswers = 0, totalQuestions = 0, wrongAnswers = 0;
response = 0;
srand(time(NULL));
while (response != -1111) {
if (question == 0){
response = addition();
} else if (question == 1){
response = subtraction();
}else if (question == 2){
response = multiplication();
}else {
response = division();
}
if (response == 0){
wrongAnswers++;
totalQuestions++;
}
if (response == 1){
correctAnswers++;
totalQuestions++;
}
}
wrongAnswers = (totalQuestions - correctAnswers);
printf("Out of %d questions, you got %d right and %d wrong.\n", totalQuestions, correctAnswers, wrongAnswers);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire