我是一名C++初学者,我刚刚编写了这个简单的程序:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么输出是这样的:
Insert a number: 3
Insert a number: 4
The sum is: 7
Run Code Online (Sandbox Code Playgroud)
而不是像:
Insert a number: 3Insert a number: 4The sum is: 7
Run Code Online (Sandbox Code Playgroud)
因为在readNumber功能中没有endl;.
我错过了什么?
(当然我对我得到的结果很满意,但这对我来说意外)
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |