My little stupid mathquestion

Started by
7 comments, last by WaterMelon34 21 years, 1 month ago
Hi, I''m making a stupid mathgame to get me started in the game programming world I am having minor problems with events. I have an addition question 753 + 13 and you have type in the answer. The problem is that no matter what you type in for the answer both events turn on and it says: Correct! Incorrect Here''s the code: typedef unsigned short USHORT; #include <stdio.h> #include <stdlib.h> USHORT FindSum(USHORT length, USHORT width); int main(int argc, char *argv[]) { USHORT heightOfYard; USHORT lengthOfYard; USHORT widthOfYard; USHORT sumOfYard; printf("MathQuest by: Alex Klos\n\n"); printf("Hit ''ENTER'' to proceed on the question."); printf("\n\nThe first number is: 13\n"); system("pause"); printf("\nThe second number is: 753\n"); system("pause"); printf("Add the numbers and type your answer: "); scanf("%d", &heightOfYard); if (heightOfYard = 766); printf("\nCorrect\n"); if (heightOfYard = 760); printf("\nIncorrect\n"); if (heightOfYard = 761); printf("\nIncorrect\n"); if (heightOfYard = 762); printf("\nIncorrect\n"); if (heightOfYard = 763); printf("\nIncorrect\n"); if (heightOfYard = 764); printf("\nIncorrect\n"); if (heightOfYard = 765); printf("\nIncorrect\n"); if (heightOfYard = 767); printf("\nIncorrect\n"); if (heightOfYard = 768); printf("\nIncorrect\n"); if (heightOfYard = 769); printf("\nIncorrect\n"); if (heightOfYard = 770); printf("\nIncorrect\n"); if (heightOfYard = 771); printf("\nIncorrect\n"); system("PAUSE"); return 0; } //As you see I''m using lengthOfYard and all that but it should work anyway. Shouldn''t it?//
Advertisement
You might want to try == instead of = if you are testing for equality in those if statements.
Thanks for trying to help but it didn''t work.
use == instead =,and learn the "else" statement.
Yes, use "==" to test for equality.

Also, you should get rid of most of those ifs and just put:

if(heightOfYard != (13+753)) {
printf("\nIncorrect!");
}

This means if heightOfYard is NOT EQUAL to (13+753) then print Incorrect. Why do the math yourself and put 766, let the computer do the math for you!

Just keep at it, study those books on C and be patient, it takes a long time to master any programming language.

Regards,
Jeff
You see I am trying to make it that if someone types:
766 and presses enter it contintues the program and tells the person that he/she is correct. And, I don''t think anyone would be stupid enough to type the answer below 760 or above 771 so if the type a wrong answer within that it''s incorrect.
Thanks alot guys it works now.
I''m confused. Why don''t you just say:

if(heightOfYard == 766)    printf("Correct\n");else    printf("Incorrect\n"); 


But the problem you''re having is the semicolon after every if. Take those out, and use the = to == suggestion (because that is the other problem) (ie this:

if (heightOfYard = 766);printf("\nCorrect\n"); 


becomes this:

if (heightOfYard == 766)printf("\nCorrect\n"); 
"You see I am trying to make it that if someone types:
766 and presses enter it contintues the program and tells the person that he/she is correct. And, I don''t think anyone would be stupid enough to type the answer below 760 or above 771 so if the type a wrong answer within that it''s incorrect. "

Never assume anything about the user. The user might not know anything about math! Furthermore, what will happen if he enteres 772? No text is printed!

If you are trying to become a serious programmer, you should always plan for as many possibilities (no matter how stupid) that you can think of. In the case of a user entering a number there are an infinite number of things that he can enter.

Now does this mean you have to code an infinite number of if''s? No, this is the beauty of programming languages, you can let the computer do the thinking for you.

You should learn a little bit more about conditional operators. More than just equals (==) comparison there are:
- not equals to (!=)
- greater than (>)
- less than (<)
- greater than or equal to (>=)
- less than or equal to (<=)

I''m sure you can find these in any introductory book on C or C++.

Regards,
Jeff

This topic is closed to new replies.

Advertisement