Here's what I get when I compile the program you posted:
Code:
$ make foo
gcc -g -Wall -std=c99 -o foo foo.c -lpthread -lm -lpq
foo.c: In function ‘main’:
foo.c:66:15: error: expected expression before ‘)’ token
foo.c: In function ‘again’:
foo.c:161:9: error: continue statement not within a loop
foo.c:165:9: error: break statement not within loop or switch
make: *** [foo] Error 1
Line 66: This is a while loop with no condition, but you note that you're aware of the problem and will fix it later.
Lines 161 & 165: The continue keyword only works in a loop, and the break keyword in a loop or a case statement. You have them in an if/else.
Also, you use
Code:
do {
...
} while (0==0);
for an infinite loop. It's far more common, and easier to read, if you just put a 1 in there instead of 0==0, like
Code:
do {
...
} while (1);
But you don't need the infinite loops with continue/break statements. You can just use the loop condition:
Code:
do {
...
} while (user gave the wrong answer);
You will need two do-while loops, one in the other. Though since you use the same loop that checks for the right answer, in each problem type (addition, subtraction, etc), I would move that into a function. My recommendation would be something like the following:
Code:
void check_answer(int correct)
{
do {
get the answer from user
if answer != correct
print wrong message
else // note, no need for else-if, it's either wrong or right
print correct message
} while answer != correct
}
int main(void)
{
...
do {
ask a problem
check_answer(num1 + num2); // pass in the right answer, num1 - num2 for subtraction, etc
} while (again()) // if again returns 1, meaning the user wants to play again, the loop will repeat
...
}
// this function returns 1 if the user wants to play again, or 0 otherwise
// so it can be used in loop condition
int again(void)
{
ask user if they want to play again
read response
if (toupper(response) == 'Y') // that way you handle upper and lower case 'y'
return 1;
else
return 0;
}
Remember, you need to be careful about any leftover newlines in your input buffer, just like we talked about in your other thread.
Last thing, you seem to be a little over-zealous with the "else if" construct. Not every if needs to be followed by an else-if. An else-if is only needed when you have more than two possibilities. In the above example, the answer is either right or wrong, there is no third option, so you only need an if/else, not an if/else-if. If it's wrong, make the user guess again, otherwise, congratulate them. A good example of when to use the else if is your main menu choice, which could be one of 5 different values. I would add a 6th option to quit the program, add that to my if-else, and also add an else at the end to handle errors:
Code:
if choice == 1
add
else if choice == 2
subtract
...
else if choice == 5
smart stuff
else if choice == 6
quit
else // this covers every other possibility except 1..5
error