an int plus an int will always be an int. Have the program do the calculation and store it in an int called temp. Then get user input in an int called answer. Then compare the two numbers using the equals operator within an if conditional with the body of the loop having an output statement indicating the answer was correct.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
int input;
const int ANSWER = random_integer1 + random_integer2;
do
{
cin >> input; // get input
} while(input != ANSWER);
/* this will repeat as long as input is not equal to the answer. when the correct value is entered, the loop won't run */
cout << "Well done or something:p";
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
replace these "
cin >> answer;
cout << "\n";
return 0;
} "
with that code. You will need to return 0 and close the brace of course :p
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
No problem. Post back if you don't get it working.
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
const int ANSWER = random_integer1 + random_integer2;
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1;
should be
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1;
const int ANSWER = random_integer1 + random_integer2;
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
ANSWER should not be declared as const. const should only be used when the value of the variable is to be hardcoded at compile time and never changed during the program. The whole point of this program is to get arbritrary numbers so by definition of the problem the value of the answer isn't known at compile time.
Of you only want to do one comparison per run of the program then use an if/else statement.
if(input != ANSWER)
//blah blah
else
//blah blah
If you want to do the comparison over and over then use a loop with the idisplay, input, calculations, comparisons, etc within the body of the loop. You can use a loop to do specified number of comparisons or you can use a loop to leave it open ended and have the user enter a specific code which needs to be evaluated to determine how many comparisons they wish to do.
Also, learn how to use code tags when posting code to this board. The watermark in the Message box and one of the anouncements at the beginning of this board tell you how.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
you're code is saying if input not equal to ANSWER then output Great Job. I doubt that's what you want.
delete this line:
(input != ANSWER);
it does nothing so it shouldn't be there.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned)time(0));
int random_integer1 = 1 + rand() % 500;
int random_integer2 = 1 + rand() % 500;
int ANSWER = random_integer1 + random_integer2;
cout << setw (6) << random_integer1 << endl;
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
int input; // declare it when you need it. This is C++ not C
do
{
cin >> input;
if(input != ANSWER)
cout << "wrong\n";
} while(input != ANSWER);
cout << "Great Job\n";
return 0;
}
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
Don't you want if input == ANSWER then output "great job".
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Do you have any book that you learn from?
edit:
the way you did it, it will ask only once for the answer and the program will terminate. In my version it will keep asking until you enter the answer.
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8