Radu
Radu

Reputation: 540

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++. 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.

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.

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
I looked on stackoverflow and I didn't seem to see anything similar, if this is a duplicate I apologize.

Upvotes: 3

Views: 1468

Answers (1)

ColinDave
ColinDave

Reputation: 530

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.

Upvotes: 3

Related Questions