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

Started by
5 comments, last by Eric Burnett 19 years, 6 months ago
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; }
Advertisement
Also, could someone tell me a way to output number correct vs. number wrong?
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.
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.
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?
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.
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.
--Eric BurnettI know I have mnemophobia, but I can't remember what it is.

This topic is closed to new replies.

Advertisement