简介
我正在尝试用 C++ 为 bin/oct/dec/hex 数字编写一个转换器。首先,我在控制台中打印一条消息,要求用户插入他想要执行的转换类型,然后是 cin
这允许他输入转换,然后我问他后面是 cin
的数字。允许他输入号码。
我的问题
我的问题是,在用户插入转换后,变量会在我没有告诉它的情况下打印在控制台上。
我尝试过的
我查看了文档,在他们的示例中,他们是这样做的:
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
这类似于我的代码(您将在下面看到)。
我也尝试做 getline(cin, type)
但如果我不编写代码,这些变量仍然会被打印出来。
我的代码
错误代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type;
int n;
cout << "Insert what type of conversion you want to make(binoct, bindec, binhex, octbin, octdec, octhex, decbin, decoct, dechex, hexbin, hexoct, hexdec): ";
cin >> type;
cout << "Insert the number you want to convert: ";
cin >> n;
return 0;
}
输入
binoct
1001
输出
Insert what type of conversion you want to make(binoct, bindec, binhex,octbin, octdec, octhex, decbin, decoct, dechex,hexbin, hexoct, hexdec):binoct
binoct
Insert the number you want to convert:1001
1001
预期输出:
Insert what type of conversion you want to make(binoct, bindec, binhex,octbin, octdec, octhex, decbin, decoct, dechex,hexbin, hexoct, hexdec):binoct
Insert the number you want to convert:1001
附加说明
我应该提一下,在这个版本的代码之前我确实使用了 cout
打印我的变量以查看它是否有效,但我重新构建了几次代码,现在没有 cout << type
或 cout << n
在我的代码中
我查看了 stackoverflow,但似乎没有看到任何类似的东西,如果这是重复的,我深表歉意。
最佳答案
代码很好,可以清理并重建您的项目,或者它与打印出值的项目/调试设置有关。
尝试在 Release模式下构建和运行程序。
关于C++ cin 在没有我告诉它这样做的情况下打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558108/