Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WaterMelon34

My little stupid mathquestion

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?//

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
"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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!