954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help on random numbers and adding them and displaying statement

I am trying to write a program that can be used to display two random numbers to be added together. This program should wait for the answer to be input. If the answer is correct display a statement that says that is correct. If the answer is wrong it needs to display that is incorrect then display the correct answer.

Thanks.

Here is what I have so far. I can not figure how the display part to display the statements and the correct answer if need be. Go easy I am new at this and it is my first attempt at programming

#include
#include
#include
#include
using namespace std;

int main ()
{
srand((unsigned)time(0));
int random_integer1;
int random_integer2;
double answer;

random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1;
cout << setw (6) << random_integer1 << endl;
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
cin >> answer;
cout << "\n";

return 0;

}

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 
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.

What would the code be. I have tried and tried but can not seem to find anything. I never really thought programming was this hard.

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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
 
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";

Where would I add this in?

Thanks for everyones help

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

replace these " cin >> answer; cout << "\n";

return 0;

} "

with that code. You will need to return 0 and close the brace of course :p

Will try it in a few. I am installing visual stuidos right now.

Thanks again.

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 
No problem. Post back if you don't get it working.

Ok try it but did not work.

Here is my new code

#include
#include
#include
#include
using namespace std;

int main ()
{
srand((unsigned)time(0));
int random_integer1;
int random_integer2;
int input;
const int ANSWER = random_integer1 + random_integer2;


random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1;
cout << setw (6) << random_integer1 << endl;
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
cin >> input; // get input
while(input != ANSWER);

return 0;

}

I am getting a debug error

ran time check failure #3 the variable 'random_integer1' is being used without being initalized

another error replacing random_integer1 with random_integer2

I can ignore these erros and it will generate the random numbers and allow an answer to be put in but that is it.

Thanks.

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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
 
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;

Ok that all worked great. Now what would I do if I want to make a statement if the enter the correct answer it produces a statement that says congratulations, but if they enter the wrong answer I get a statement that says that answer is wrong, but the correct answer is and then gives the answer?

Thanks for your help.

This is the new code

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main ()
{
srand((unsigned)time(0)); 
int random_integer1; 
int random_integer2;
int input;
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1;  
const int ANSWER = random_integer1 + random_integer2;

cout << setw (6) << random_integer1 << endl; 
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
cin >> input; 
(input != ANSWER);


return 0;

}
davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

Ok still not getting it. No matter what answer I put in it still says great job

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main ()
{
srand((unsigned)time(0)); 
int random_integer1; 
int random_integer2;
int input;
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1; 

int ANSWER = random_integer1 + random_integer2;

cout << setw (6) << random_integer1 << endl; 
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
cin >> input; 
(input != ANSWER);


if(input != ANSWER)
  cout << "Great Job";
else
  cout << "Wrong";


return 0;

}
davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

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.

Yea I took that line out but still not getting it

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

int main ()
{
srand((unsigned)time(0)); 
int random_integer1; 
int random_integer2;
int input;
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1; 

int ANSWER = random_integer1 + random_integer2;

cout << setw (6) << random_integer1 << endl; 
cout << "+ ";
cout << setw (4) << random_integer2 << endl;
cout << "------";
cout << "\n";
cin >> input; 
cout << "\n";



if (input != ANSWER)
  cout << "Great job" << endl;
else 
  cout << "Wrong" << endl;


return 0;

}
davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

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.

I do have a book but it does a poor job explaining anything and my teacher I can hardly understand.

davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
Don't you want if input == ANSWER then output "great job".

Ok got it working. Just now have to figure out how to make a statement showing the correct answer if I put in the wrong one.

<pre><code>#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;iomanip&gt;
#include &lt;ctime&gt;
using namespace std;

int main ()
{
srand((unsigned)time(0)); 
int random_integer1; 
int random_integer2;
int input;
random_integer1 = (rand()%500)+1;
random_integer2 = (rand()%500)+1; 

int ANSWER = random_integer1 + random_integer2;

cout &lt;&lt; setw (6) &lt;&lt; random_integer1 &lt;&lt; endl; 
cout &lt;&lt; &quot;+ &quot;;
cout &lt;&lt; setw (4) &lt;&lt; random_integer2 &lt;&lt; endl;
cout &lt;&lt; &quot;------&quot;;
cout &lt;&lt; &quot;\n&quot;;
cin &gt;&gt; input; 
cout &lt;&lt; &quot;\n&quot;;

 	
		
if 
(input == ANSWER)
cout &lt;&lt; &quot;Great Job\n&quot;; 	
else
cout &lt;&lt; &quot;Wrong\n&quot;;
return 0;

}</code></pre>
davids2004
Light Poster
43 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You