0

I am starting to learn C++ and I am trying to create a program that asks the user to add values for all primitive types in C++, here is what I have written so far:

#include <iostream>
using namespace std;

int main()
{
 bool empty_bool;
 char empty_char;
 int empty_int;
 float empty_float;
 double empty_double;
 cout << "Please enter a value for all inbuilt primitive types in C++" << endl;
 cout << "Boolean:";
 cin >> empty_bool;
 cout << "Char";
 cin >> empty_char;
 cout << "Int";
 cin >> empty_int;
 cout << "float";
 cin >> empty_float;
 cout << "double";
 cin >> empty_double;
}

The problem is that my program takes the input of boolean but then it just prints the rest of the variable names but it doesn't allow to take the value for the said variable and I can't figure out why, what am I doing wrong here?

Mahma Deva
  • 546
  • 2
  • 9
  • 23
  • 4
    Did you write something like `true` as input for the boolean? That is not going to work. You need to input `0` (for false) or `1` (for true). – walnut Oct 20 '19 at 16:29
  • What do you enter for Boolean? Unless `boolalpha` flag is set (and by default it is not), `cin >> empty_bool` expects an integer in the input, and interprets zero as `false` and non-zero as `true` – Igor Tandetnik Oct 20 '19 at 16:30
  • In C++ bool is internally an integral value. So you have to enter 1 for true and 0 for false. – jignatius Oct 20 '19 at 16:33
  • Yes I entered true, instead of 1 or 0, coming from a JavaScript background I didn't thought about that, thanks! – Mahma Deva Oct 20 '19 at 16:34
  • @IgorTandetnik My GCC accepts only `0` and `1`, no other numbers as input for `bool` (with `std::noboolalpha`) – walnut Oct 20 '19 at 16:34
  • @uneven_mark Of course. `std::noboolalpha` turns `boolalpha` flag off (hence "no" in front). – Igor Tandetnik Oct 20 '19 at 16:36
  • @IgorTandetnik I was referring to your comment "*expects an integer in the input*" and "*and non-zero as true*". It seems to accept only `0` and `1`, no other integers. – walnut Oct 20 '19 at 16:38
  • @uneven_mark Ah. Indeed, only 0 and 1 should be accepted, anything else sets `failbit`. – Igor Tandetnik Oct 20 '19 at 16:40

1 Answers1

1

After doing any input, cin will contain a result condition. Reading invalid input will put the input stream cin into a fail state; typically, you are supposed to check that by evaluating cin in a boolean context:

if (cin >> empty_int)
    cout << empty_int; // empty_int here is not empty anymore
else
    cout << "error";

When reading an inappropriately formatted value for bool (as mentioned in comments, it must be 0 or 1 or true or false depending on the boolalpha flag), cin remembers its error state, and will not do any further input until it is reset:

cin.clear();

Note by user4581301:

Note that clear does not remove the offending data from the stream. Depending on what was read and how before the failure, you probably have something in the stream that will cause the exact same error again. If so, remove it before continuing, using one of the following ideas:

  • use std::istream::ignore (like here)
  • read it into something permissive like a std::string
  • or whatever best fits your usage
anatolyg
  • 26,506
  • 9
  • 60
  • 134