CIN正在吞噬输出流

cin is eating output stream

本文关键字:输出流 吞噬 CIN      更新时间:2023-10-16
#include <iostream>
using namespace std;
void main(int argc, char* argv[])
{
    int conversion, hash;
    cout << "Select one." << endl;
    cout << "0: Radix Method 32" << endl;
    cout << "1: Radix Method 64" << endl;
    cout << "2: SumUp" << endl;
    cin >> conversion;
    cout << endl << "Select one." << endl;
    cout << "0: Division" << endl;
    cout << "1: Multiplication" << endl;
    cin >> hash;
    cout << "Conversion: " + conversion << endl;
    cout << "hash: " + hash << endl;
}

就这么简单,我得到了疯狂的输出。 我觉得这是显而易见的,但我太累了,看不到它。 我在变量中输入的数字是从下一个输出字符串中删除的字符数。 前任:

Select one.
0: Radix Method 32
1: Radix Method 64
2: SumUp
1
Select one.
0: Division
1: Multiplication
2
onversion:
sh:
Press any key to continue . . .
Select one.
0: Radix Method 32
1: Radix Method 64
2: SumUp
5
Select one.
0: Division
1: Multiplication
1
rsion:
ash:
Press any key to continue . . .

我是疯了还是这毫无意义?我用错cin了吗? 我已经几个月没有使用C++了,但我看不出有什么问题。

cout << "Conversion: " + conversion表示从数组头部之后的conversion元素打印。

您可能希望这样做(将+更改为<<):

cout << "Conversion: " << conversion << endl;
cout << "hash: " << hash << endl;