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;

}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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.

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.

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";
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

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

return 0;

} "

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

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.

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.

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

}

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.

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;

}

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.

#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;
}

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;

}

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.

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.

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.

#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\n";    
    else
        cout << "Wrong\n";

    return 0;    
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Ok got everything working. Thanks for all the great help here.

#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 << "Congratulations\n";  
else
cout << "That is incorrect\n";
//Display the correct answer
cout << "The correct answer is " << ANSWER;
cout << "\n";
return 0;

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

So your book isn't good.

If you have money and can buy online I recommend Beginning C++ Game programming (it's not only for game programming). It's a great book for beginners.

If you can't buy it, Thinking in C++ is a free book available hereDownload volume 1.

Ok now I have it correct. The code above would display the correct answer regardless if the correct answer was input or not. The following code only displays the correct answer if the wrong one is input.

Thanks again

#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 << "Congratulations\n"; 	
else
cout << "That is incorrect,\nThe correct answer is: " << ANSWER;
cout << "\n";
return 0;

}

So your book isn't good.

If you have money and can buy online I recommend Beginning C++ Game programming (it's not only for game programming). It's a great book for beginners.

If you can't buy it, Thinking in C++ is a free book available hereDownload volume 1.

I will take a look at those when I get home.

This question has already been answered. Start a new discussion instead.