Reputation: 47
I'm trying to figure out what's wrong with my checkQuestion
function. I don't know why but sometimes it won't check certain questions or completely skip them. I created a function createQuestion
and I called checkQuestion
in createQuestion
so I don't know if that what's causing everything to mess up. Any help would be appreciated and I just need some clues as to whats wrong. I posted the link to the program if you want to run it.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void) {
srand(time(NULL));
printf("Math quiz. \n");
printf("This is level 1. There are a total of 10 questions. \n\n");
createQuestion();
checkQuestion();
}
//Functions
int integers(void) {
int digit;
digit = rand() % 10;
return digit;
}
char operations(void) {
int digit;
digit = rand() % 4;
if (digit == 0) {
return '+';
} else if (digit == 1) {
return '-';
} else if (digit == 2) {
return '*';
} else if (digit == 3) {
return '/';
}
}
int createQuestion(void) {
int i;
int count = 0;
int answer;
int sum;
for (i = 1; i <= 10; i++) {
count++;
printf("%d)%d", count, integers());
printf("%c", operations());
printf("%d", integers());
printf("=");
scanf("%f", & answer);
checkQuestion(integers(), integers(), operations(), answer);
}
}
void checkQuestion(float a, float b, float c, char d) { //a integer b integer c answer //d operator
int answer1;
int answer2;
int answer3;
int answer4;
if (operations() == '+') {
answer1 == a + b;
answer1 == c;
if (answer1 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '-') {
answer2 = a - b;
if (answer2 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '*') {
answer3 = a * b;
if (answer3 == c) {
return messagesGood();
} else {
return messagesBad();
}
} else if (operations() == '/') {
answer4 = a * b;
if (answer4 == c) {
return messagesGood();
} else {
return messagesBad();
}
}
}
void messagesGood(void) {
int digit;
digit = rand() % 4;
switch (digit) {
case 0:
printf("Very good! \n");
break;
case 1:
printf("Excellent! \n");
break;
case 2:
printf("Nice Work! \n");
break;
case 3:
printf("Keep up the good work! \n");
break;
}
}
void messagesBad(void) {
int digit;
digit = rand() % 4;
switch (digit) {
case 0:
printf("No. Please try again. \n");
break;
case 1:
printf("Wrong. Try once more. \n");
break;
case 2:
printf("Don’t give up! \n");
break;
case 3:
printf("No. Keep trying. \n");
break;
}
}
Upvotes: 0
Views: 54
Reputation: 582
One weird thing is, you have a function that returns a char:
**char** operations(void){
etc...
}
then you put it in a function that expects a float:
checkQuestion(integers(), integers(), **operations()**, answer);
void checkQuestion(float a, float b, **float c**, char d)
Upvotes: 0
Reputation: 4218
Every time you call operations
, you could get a different result. You need to compare against d
in your conditions, not against operator
. You have a similar issue in createQuestion
, where what is passed to checkQuestion
is probably not what was shown to the user.
For example:
if (d == '+') {
// ...
}
Upvotes: 1