c++ - cin 正在吃输出流

标签 c++ cout cin

#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;

关于c++ - cin 正在吃输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35860049/

相关文章:

c++ - 为什么 std::bad_cast 被 boost::locale 抛出?

Windows ME 上的 C++

C++如何将char作为数字而不是字符

c++ - ios_base::sync_with_stdio(false) 在来自标准输入的两个输入之间不起作用

c++ - 由于 istream 中的杂散\n 而不是 scanf() 或 cin,gets () 的结果不正确?

c++ - 像自定义类一样构造的指针(内置类型)如何工作?

c++ - Qt Creator 未定义套接字 - Linux

C++ cout 十六进制值?

c++ - 为什么 cout.precision() 会增加 float 的精度?

c++ - 使用 std::cout 打印零填充十六进制