Jump to content
  • Advertisement
Sign in to follow this  
Nongan

Help w/ my program in c++ (randum numbers generating math equations)

This topic is 5461 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

Im building a program for tommorrow in my computer class. Could someone tell me how to make my first number greater than my second number? Here is the source code so far. #include <iostream> #include <conio.h> #include <stdio.h> #include <time.h> #include <stdlib.h> using namespace std; main() { int num1, num2; char op; int i, ans, guess; int b; for (b=1; b<=3; b++) { srand(time(NULL)); for (i=1; i <=1; i++) { num1 = rand()%12; } int c; for (c=1; c <=1; c++) { ; num2 = rand()%12; } switch(rand()%4) { case 0: op = '+'; ans = num1 + num2; break; case 1: op = '-'; ans = num1 - num2; break; case 2: op = 'x'; ans = num1 * num2; break; case 3: op = '/'; ans = num1 / num2; break; } cout << num1 << endl << op <<" " << num2 << endl <<"__________" << endl << "Press enter once, then answer the question: "; getch(); cin >> guess; if(ans == guess) { cout << "Good Job" << endl; } else if(ans) { cout << "Wrong!" << endl; } getch(); } return 0; }

Share this post


Link to post
Share on other sites
Advertisement
I don't understand the point in the for() loops... You could just do this:

num2 = rand()%12;
do {
num1 = rand()%12;
} while(num1 < num2);



EDIT:
For outputing the answers:
cout << "Correct answer: " << ans << endl;
cout << "Your guess: " << guess << endl;
Or whatever you want to combine the 2 lines.

Share this post


Link to post
Share on other sites
I took out the second and third loops. Donno why I had them. The first one is to loop the program multiple times. And I tried what you said and it still didnt work.

Share this post


Link to post
Share on other sites
Define "doesn't work". If you used the code I provided, then num1 will always be greater than or equal to num2. Or were you talking about outputting the right and wrong answers?

Share this post


Link to post
Share on other sites
Nah, I just wanted to be able to output the number of answers correct vs. number of answers wrong, cause im gonna loop it ~25 times. So say you got 20 out of 25 correct, then it will show you got 20 correct, 5 wrong, 25 questions total, then %right.

Share this post


Link to post
Share on other sites
I cannot just tell you the answer to your homework (that helps nobody), but I can give you some tips.

1). Your program is going to randomly crash. (Hint: look at case 3)

2). What happens if the answer is 0, and the person guesses 1?

3). Not sure why you need your first number greater than your second, but Evil Steve's answer is slightly wrong. It will let them be the same, which is not what you asked for. Since he already posted it, I will go ahead and correct it. But you are not actually supposed to get anything given to you (see the faq).


num2 = rand()%12;
do {
num1 = rand()%12 + 1;
} while(num1 <= num2);




4). How do you keep track of the number correct? Use a variable of course. You have lots of extra ones around. That and a loop, and everything is easy.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!