I'm a C++ beginner and I just wrote this simple program:
#include <iostream>
using namespace std;
int readNumber()
{
cout << "Insert a number: ";
int x;
cin >> x;
return x;
}
void writeAnswer(int x)
{
cout << "The sum is: " << x << endl;
}
int main()
{
int x = readNumber();
int y = readNumber();
int z = x + y;
writeAnswer(z);
return 0;
}
I don't understand why the output is like this:
Insert a number: 3
Insert a number: 4
The sum is: 7
and not like:
Insert a number: 3Insert a number: 4The sum is: 7
since in the readNumber
function there is no endl;
.
What am I missing?
(Of course I'm happy with the result I get, but it's unexpected for me)