hi all,
i need help with writing a program for adding two random numbers. my output should look something like this:

Enter a number between 1 and 1 million: 6758
Enter another number between 1 and 1 million: 87698

376
+ 486
_____
862

Congratulations! (smiley face)
You're pretty good!

Any help is apperiaciated..
many thx in advance

That is ridiculously easy. It will take 5 minutes of thinking or one google search to do.

WE WILL NOT DO YOUR HOMEWORK FOR YOU.

If your stuck, show us something (an attempt) and then we can point you in the right direction.

>yeah, i have!
So do we have to somehow divine what problem you're having or are you going to post your code and tell us?

heres a starting point

// my first program in C++
 
#include <iostream>
 
using namespace std;i
 
int main ()
{ 
 cout << "Hello World!"; 
 return 0;
}

ok folks. i am including my code with this reply.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main ()
{
    
    cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
    cout << "*****************************" <<endl;
    cout << "*Created by Manthan Meghpara*" <<endl;
    cout << "*****************************" <<endl;
    
    
    cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
    
    cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
    unsigned number;
   //int = rand();
   cout << "Enter a number between 1 and 1 Million: " ;
   cin  >> number;
   
   cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
   cout << "Enter another number between 1 and 1 Million: ";
   cin  >> number;
   
   cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
   srand ( time ( 0 ) );
   int one = 1 + rand() % 1000;
   int two = 1 + rand() % 1000;

   int sum = one + two;

   int Answer;
   cout << one << endl << two << endl;
   cout << "What are the sum of the two numbers? " ;
   cin  >> Answer;

   if ( Answer == sum )
   {
    
      cout << " " << endl; //I am just skipping lines to match the exact output as you asked.  
      cout << "Congratulations!" <<endl;
      cout << "You're pretty good!" <<endl;
   }
   else
   
      cout << " " << endl; //I am just skipping lines to match the exact output as you asked.
      cout << "The answer is incorrect, Try Again!" <<endl;
      system("PAUSE");
    return 0;
}

what i am having problem with is the middle part where i have to add two numbers to match the exact output

i dont know how to get like this

360
+ 450
_____
810


thank u all for helping!

I imagine you'd do something like this:

std::cout<< one <<"\n+"<< two <<"\n--------\n";
std::cin>> answer;

ok, i tried std code.. it worked..
but another question..
i dont want that line which says what are the sum of two numbers? i want answer to be displayed underneath the addition of two numbers

>i dont want that line which says what are the sum of two numbers?
Then take it out. My example was meant to replace these lines:

cout << one << endl << two << endl;
cout << "What are the sum of the two numbers? " ;
cin >> Answer;

hi there,

it did worK! but, another problem. when i enter the answer, i get the message for incorrect answer too even though answer is correct. any thoughts? I also dont know how to fixed allignment for two random numbers? i want both numbers to be displayed right underneath one another..

thx so much for helping out

>i get the message for incorrect answer too even though answer is correct. any thoughts?
You forgot to wrap the else clause in braces.

Hey Narue,

It worked. many many thx for all the help. One last thing, any idea on how to write the two adding numbers right underneath one another? that would be gr8.. thx again

>any idea on how to write the two adding numbers right underneath one another?
I have no idea what you're asking. Give me a detailed example, because what I've given you so far does exactly what you've described.

Ok, let me explain. The program runs fine with no error, which is great. Only part i need help with is, when two random number comes up for adding! like below

320
+ 120
______
440

What i am asking is that, number 3,1, and 4 should be under each other. same place. i hope you understand what i am asking

Oh. Okay, change this:

std::cout<< one <<"\n+"<< two <<"\n--------\n";

To this:

std::cout<<"  "<< one <<"\n+ "<< two <<"\n--------\n  ";

gr8, it worked!! I am really thankful to you for all this help! Do you know by any chance how can I add a smiley face next to Congratulations in my output?

This article has been dead for over six months. Start a new discussion instead.