C++ cin 在没有我告诉它这样做的情况下打印变量

C++ cin prints the variable without me telling it to do that

简介

我正在尝试用 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 << typecout << n 在我的代码中
我查看了 Whosebug,我似乎没有看到任何类似的东西,如果这是重复的,我深表歉意。

代码很好,可以清理并重建您的项目,或者它与您的项目/调试设置有关,打印出值。

尝试在发布模式下构建和运行程序。