#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare a variable to store an integer
int InputNumber;
cout << "Enter an integer: ";
// store integer given user input
cin >> InputNumber;
// The same with text i.e. string data
cout << "Enter your name: ";
string InputName;
cin >> InputName;
cout << InputName << " entered " << InputNumber << endl;
return 0;
}
如果我为 InputNumber 输入一些字符串,上面的程序会产生错误的输出,那里发生了什么,我假设分配给 Inputnumbe 的内存被覆盖了,但这是问题所在吗?还给出了示例输出。
correct output
Enter an integer: 123
Enter your name: asdf
asdf entered 123
wrong output
Enter an integer: qwert
Enter your name: entered 0