gpt4 book ai didi

c++ - cout 打印不正确的值

转载 作者:行者123 更新时间:2023-11-30 03:26:30 25 4
gpt4 key购买 nike

所以,我正在尝试制作一个基本的 C++ 程序来转换 Base 10 -> Base 16

while(true){
int input;
cout << "Enter the base 10 number\n";
cin >> input;
cout << "The hex form of " << input << " is: ";
int decimal = input;
cout << hex << uppercase << decimal << "\n";
decimal = NULL;
input = NULL;
Sleep(3000);
}

并且在第一次运行时它就起作用了。例如,如果我输入 7331,我会得到:

The hex form of 7331 is 1CA3

但如果我第二次尝试(使用相同的输入),我会得到:

The hex form of 1CA3 is 1CA3

我可以用任何整数输入来尝试这个,它在第一次运行时工作正常,但之后它两次输入 16 进制数。

最佳答案

您需要重置您的流。当您将 std::hex 应用于 std::cout 时,它会永久应用于流 (std::cout),您需要对其进行重置。您只需将程序更改为:

cout << "The hex form of " << std::dec << input << " is: ";

关于c++ - cout 打印不正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48292173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com