| | |
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:
Solved Threads: 0
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;
}
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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
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.
C++ Syntax (Toggle Plain Text)
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";
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
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";
Thanks for everyones help
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
const int ANSWER = random_integer1 + random_integer2; random_integer1 = (rand()%500)+1; random_integer2 = (rand()%500)+1;
should be
C++ Syntax (Toggle Plain Text)
random_integer1 = (rand()%500)+1; random_integer2 = (rand()%500)+1; const int ANSWER = random_integer1 + random_integer2;
![]() |
Other Threads in the C++ Forum
- Previous Thread: create a class?? help!
- Next Thread: a program about subcolletions
Views: 1461 | Replies: 23
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






