Using switch with double or bool


This Content is from Stack Overflow. Question asked by raelyn

Can someone please help me with this. This is the code I currently have to handle invalid input. It’s doing what I want it to for the most part but I just found out I’m require to use switch. I’ve been trying to figure out how I could since I’m using double and bool. I’m also required to use double. I also don’t under get how I would use cases for this.

double num;
bool number = false;
  while (!number)
    // Get initial input from the user.
    cout << "Enter a number: ";
    cin >> num;
    cout << endl;
    cin >> num;
    if (
    cin.ignore(100000, 'n');
    cout << "Error: A number was not entered." << endl;
    cout << "Enter a valid number:";
    cin >> num;
    number = true;
    cout << num << endl;
    return 0;


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?