I am taking a course on C++ game dev and one of the exercises asks me to write a program that:

asks the user to input two numbers.
compute those numbers, +, -, *, %.
and then display the results.

It took me forever and a minute, but I got it. But how do I get the command window to display the number the user inputs instead of the n1 and n2?
(i.e: 8 + 4 = 12, not n1 + n2 = 12)

``````#include <iostream>
#include <string>

using namespace std;

int main()
{
int n1;
int n2;

cout << "please enter a number:       ";
cin >> n1;
cout << "please enter another number: ";
cin >> n2;

cout << "n1+ n2 = "<< n1+n2  <<endl;
cout << "n1 - n2 = " << n1-n2 <<endl;
cout << "n1 * n2 = " << n1*n2 <<endl;
cout << "n1 % n2 = " << n1%n2 <<endl;

}``````

## All 4 Replies

``cout << n1 << " + " <<  n2 << " = " << n1+n2  <<endl;``

AWESOME! Ty wolfpack. I guess I could have figured that out if I kept at it for another few months.

Can you answer another noob ? for me...

In the line of code:

``cout << n1 << " + " <<  n2 << " = " << n1+n2  <<endl;``

why do you have to 'state' the <<n1+n2 if it has already been 'stated' before in that line?

thanx

> why do you have to 'state' the <<n1+n2 if it has already been 'stated' before in that line?
in your c++ program, you have to say precisely what is it that you want done. `cout << n1 << " + " << n2 << " = "` 'states' that you want the value of n1, a + sign, the value of n2 and an = sign to be sent to stdout.
you also need to 'state' that the value of n1+n2 and a newline are to be sent to stdout after the = sign.

Got it!
TY vijayan121.

Man this C++ stuff is EZ...:sweat:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.