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

标签 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,但似乎没有看到任何类似的东西,如果这是重复的,我深表歉意。

最佳答案

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

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

关于C++ cin 在没有我告诉它这样做的情况下打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558108/

相关文章:

c++ - 分小部分读取大型二进制文件

c++ - 连接客户端时如何在QT中更新QGraphicsView?

c++ - 如何在 C++ 中将对象属性设置为先前初始化的数组

Java RegEx - 如何将每个 Stringchar 转换为 *?

c++ - 允许用户跳过输入并直接按 Enter 键

C++ getline cin 问题

c++ - 私有(private)基类和多重继承

html - Input type=number Safari 仍然允许带步进器的字母

java - 将某些字符/键的输入重新映射到 JTextComponent 的最佳方法?

C++ cin 和 strnicmp 不工作