Need Help on random numbers and adding them and displaying statement

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Need Help on random numbers and adding them and displaying statement

 
0
  #1
Nov 17th, 2008
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 <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
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;

}
Last edited by davids2004; Nov 17th, 2008 at 12:18 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #2
Nov 17th, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #3
Nov 17th, 2008
Originally Posted by Lerner View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Need Help on random numbers and adding them and displaying statement

 
1
  #4
Nov 17th, 2008
  1. int input;
  2. const int ANSWER = random_integer1 + random_integer2;
  3.  
  4. do
  5. {
  6. cin >> input; // get input
  7. } while(input != ANSWER);
  8. /* this will repeat as long as input is not equal to the answer. when the correct value is entered, the loop won't run */
  9.  
  10. cout << "Well done or something:p";
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #5
Nov 17th, 2008
Originally Posted by minas1 View Post
  1. int input;
  2. const int ANSWER = random_integer1 + random_integer2;
  3.  
  4. do
  5. {
  6. cin >> input; // get input
  7. } while(input != ANSWER);
  8. /* this will repeat as long as input is not equal to the answer. when the correct value is entered, the loop won't run */
  9.  
  10. cout << "Well done or something:p";
Where would I add this in?

Thanks for everyones help
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #6
Nov 17th, 2008
replace these "
cin >> answer;
cout << "\n";

return 0;

} "

with that code. You will need to return 0 and close the brace of course :p
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #7
Nov 17th, 2008
Originally Posted by minas1 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #8
Nov 17th, 2008
No problem. Post back if you don't get it working.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: davids2004 is an unknown quantity at this point 
Solved Threads: 0
davids2004 davids2004 is offline Offline
Light Poster

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #9
Nov 17th, 2008
Originally Posted by minas1 View Post
No problem. Post back if you don't get it working.
Ok try it but did not work.

Here is my 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;
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 81
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 8
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: Need Help on random numbers and adding them and displaying statement

 
0
  #10
Nov 17th, 2008
  1. const int ANSWER = random_integer1 + random_integer2;
  2.  
  3.  
  4. random_integer1 = (rand()%500)+1;
  5. random_integer2 = (rand()%500)+1;


should be

  1. random_integer1 = (rand()%500)+1;
  2. random_integer2 = (rand()%500)+1;
  3.  
  4. const int ANSWER = random_integer1 + random_integer2;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1461 | Replies: 23
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC