Need help with cin setw

Posted on 16th Feb 2014 by admin

I made a program here is my code:

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

int main()
{
unsigned seed = 0;
int randomNum;
int randomNum1;
int number;
int answer;
srand(time(0));
randomNum = 1 + rand() % 50;
randomNum1 = 1 + rand() % 50;
number = randomNum + randomNum1;



cout << "Key in a number for the Random Number seed" << endl;
cout << "and hit the Enter key to display an addition problem. ";
cin >> seed;


cout << setw (9) << randomNum << endl;
cout << "+";
cout << setw (8) << randomNum1 << endl;
cout << setw (9) << "----" << endl;
cin >> answer; // This is what I need to be coded right!



if (answer == number)

cout << "Congratulations! That's right." << endl;

else

cout << "Sorry, the correct answer is " << number << endl;


return 0;
}






The problem is that for the cin answer I want it to be like this format:
space 49
+space 10 //ignore the space the forum wont let me do it proper but basically line
----- all the numbers up even the cin answer
space ##


I tried to do

cin >> setw (9) >> answer; and also
cin >> right >> setw (1) >> answer;

and nothing happened basically my ? is how do I get the user to input his answer in the same lineup of the other 2 random numbers generated! any help would be GREATLY APPRECIATED LOL spent like 1-2 hours looking around the web for a case like this and NOTHING! >.<

Other forums