C ++ cin会在不通知我执行该操作的情况下打印该变量

时间:2018-11-30 13:00:22

标签: c++ input cin cout

简介

我正在尝试为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在我的代码中
我查看了stackoverflow,但似乎没有类似的东西,如果我对此表示歉意,那就对不起。

1 个答案:

答案 0 :(得分:3)

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

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