Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Either way, with indentation, it is clear that the quiz taker can only get at most 1 question reported as correct. Even if he/she got the other questions correct, they would never be checked for correctness. Likewise, the quiz taker can get at most 1 question reported as incorrect.

Trace the logic. For example, suppose that add == 4. Then ++correct. But the else branch will not be followed, hence the next thing that happens is that the result is printed. But suppose that add != 4. You know that add == 4 will evaluate to false, hence the else branch will be followed. Now, add != 4 evaluates to true, hence ++incorrect. But now the else branch here will not be followed, hence the next thing that happens is that the result is printed. Since either add == 4 or add != 4, there is no way that sub == 0 will ever get checked.

Rather, you should have three separate if-else statements, each handling a question independently of the others.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.

Originally Posted by paulb39

and why does correct need two parentheses in front of if instead of one?

Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

From what I see, you want to output an int rather than a double, so this a type cast to coerce the double to int. It truncates the double in the process (i.e., the portion after the decimal point is "chopped off"), but other than that it probably does what you want.

Because I chose to use parentheses to group (correct * 100.0). This is actually unnecessary.

Thanks.

Wrote another program, does same sort of thing but I tried to make it a little long/ more complicated.

Got it to work in my first try, which is pretty awesome, but it doesn't calculate the percent correct, it always says "you scored 25 percent"