943,865 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1900
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2008
0

Need Help on random numbers and adding them and displaying statement

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 17th, 2008
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Lerner ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 17th, 2008
1

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

C++ Syntax (Toggle Plain Text)
  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";
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by minas1 ...
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 17th, 2008
0

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

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

return 0;

} "

with that code. You will need to return 0 and close the brace of course :p
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by minas1 ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 17th, 2008
0

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

No problem. Post back if you don't get it working.
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 17th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by minas1 ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
davids2004 is offline Offline
43 posts
since Nov 2008
Nov 17th, 2008
0

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

C++ Syntax (Toggle Plain Text)
  1. const int ANSWER = random_integer1 + random_integer2;
  2.  
  3.  
  4. random_integer1 = (rand()%500)+1;
  5. random_integer2 = (rand()%500)+1;


should be

C++ Syntax (Toggle Plain Text)
  1. random_integer1 = (rand()%500)+1;
  2. random_integer2 = (rand()%500)+1;
  3.  
  4. const int ANSWER = random_integer1 + random_integer2;
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: create a class?? help!
Next Thread in C++ Forum Timeline: a program about subcolletions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC