简体   繁体   English

C ++ cin在不通知我的情况下打印变量

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

Introduction 介绍

I am trying to code a converter for bin/oct/dec/hex numbers in c++. 我正在尝试为C ++中的bin / oct / dec / hex数编码转换器。 At first I print a message in a console asking the user to insert the type of conversion he wants to do and, followed by the cin that allows him to enter the conversion and then i ask him for the number followed by the cin that allows him to enter the number. 首先,我在控制台上打印一条消息,要求用户插入他想执行的转换类型,然后输入cin (允许他输入转换),然后问他输入数字,然后输入cin (允许他输入)输入号码。

My problem 我的问题

My problem is that after the user inserts the conversion, the variable gets printed on the console without me telling it that. 我的问题是,用户插入转换后,变量会在控制台上打印出来,而我却没有告诉它。

What I've Tried 我尝试过的

I looked on the docs and in their example they do it like this: 我看了看文档,在他们的示例中,他们这样做:

cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";

And that is similar to my code(which you will see below). 这类似于我的代码(您将在下面看到)。
I also tried to do getline(cin, type) but still the variables would get printed without me coding that. 我也尝试做getline(cin, type)但是如果不编写代码,变量仍然会被打印出来。

My code 我的密码

Faulty code: 错误的代码:

#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;
}

Input 输入

binoct
1001

Output 产量

 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

Expected output: 预期产量:

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

Additional notes 补充笔记

I should mention that before this version of the code I did use cout to print my variables to see if it works but i re-built the code a few times and now there's no cout << type or cout << n inside my code 我应该提到,在此版本的代码之前,我确实使用cout打印变量以查看其是否有效,但是我重新构建了几次代码,现在代码中没有cout << typecout << n
I looked on stackoverflow and I didn't seem to see anything similar, if this is a duplicate I apologize. 我查看了stackoverflow,但似乎没有类似的东西,如果这是重复的,我表示歉意。

The code is fine, either clean and rebuild your project, or it has something to do with your project / debug settings, that print out the value. 该代码很好,可以清理并重建项目,或者与项目/调试设置有关,这些代码可以打印出该值。

Try Building and Running the Program in release mode. 尝试在发布模式下构建和运行程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM