Reputation: 7
I made a simple program to add and subtract the given numbers. Let's say the 2 numbers give were 5 and 5. It would print 5 + 5 = 10 and 5 - 5 = 0. Right now I'm not sure what's wrong. I might need a temporary variable, but something with the input isn't right. If you test the numbers with 5 and 5 it prints:
Addition / Subtraction Program
*------------------------------*
Press Enter to begin!
What is the number you'd like to add / sub to?5
5
What is the next number?5
55 + 5 = 105 - 5 = 0
Here's the code I'm using:
#include <iostream>
using namespace std;
int main() {
int num_1;
int num_2;
cout << "Addition / Subtraction Program" << endl << "*------------------------------*\n\nPress Enter to begin!";
cin.get();
cout << "What is the number you'd like to add / sub to?";
cin >> num_1;
cout << num_1 << endl << "What is the next number?";
cin >> num_2;
cout << num_2;
cout << num_1 << " + " << num_2 << " = " << num_1 + num_2;
cout << num_1 << " - " << num_2 << " = " << num_1 - num_2;
return 0;
}
Upvotes: 0
Views: 2067
Reputation: 11173
Actually there is no problem with addition and subtraction. You didn't print the new line ('\n') character at the end of this line -
cout << num_1 << " + " << num_2 << " = " << num_1 + num_2;
It's good practice to add newline ('\n') character one endl
manipulator at the end of each line.
In your current code, you have only formatting problem.
Upvotes: 0
Reputation: 4023
The outputs are not incorrect. You only need to fix the formatting. In between the following two cout's, there is no clear demarcation:
cout << num_1 << " + " << num_2 << " = " << num_1 + num_2;//cout 1
cout << num_1 << " - " << num_2 << " = " << num_1 - num_2;//cout 2
Hence you get the output as:
55 + 5 = 105 - 5 = 0
|-cout 1 -||-cout 2-|
You can either use a newline (by cout<<endl;
or cout<<"\n";
between the two) or a space(cout<<" ";
) to clearly demarcate between the two outputs.
Upvotes: 0
Reputation: 45826
The output is correct, it's just missing spaces.
After the user enters their number, you echo it back to them; without any whitespace. This "turns" 5 into 55, and 10 5 into 105.
The addition and subtraction are fine, you just need to format your output. End your cout
lines with a << endl;
or << "\n";
to see the difference.
Upvotes: 6