0

Trying to create a while loop that will re-ask to enter correct input. In this case, the correct input needs to be a number. When I test it and put in a non-number answer, it ends the program. The while loop isn't working.

if (variableQuestionsVf == "Yes" || variableQuestionsVf == "yes")
                    {
                        cout << "Input final velocity (in m/s): " ;
                        while (cin >> finalVelocity)
                        {
                            istringstream s(sVf);
                            s >> value;

                            if (value <= 0 || value >= 0) //Validating input for final velocity
                            break;

                            cout << "Please enter final velocity (in m/s): " ;
                        }
                    }

Is there an easier way?

Art Byra
  • 25
  • 1
  • 1
  • 8
  • I'm curious, why do people make 1980s-style interactive programs when a C++ tool that reads from a lightly delimited text file would be way easier to run repeatedly? Even better is to embrace using `argv` when possible to communicate with your program. – tadman Feb 18 '15 at 21:06
  • by digit, you mean a single digit or any number ? – Nithin Feb 18 '15 at 21:12
  • @tadman probably because any beginner's C++ book I've seen tends to start off with i/o like that. – Eugene K Feb 18 '15 at 21:14
  • For digit I mean number -- edited to fix that – Art Byra Feb 18 '15 at 21:16
  • @tadman I am new to C++ and I was just trying to make a program in my free time. It isn't the prettiest code and it may not be the most effective but you have to start somewhere right? In the end it will work when I have all the bugs removed and that's a good start for me. – Art Byra Feb 18 '15 at 21:20
  • Not criticizing, just asking. When you're trying to debug code like this I've found using a simple test framework makes getting things right a lot easier, and way faster than having to repeatedly run and type things in. For example. [Catch](https://github.com/philsquared/Catch/blob/master/docs/tutorial.md) is super simple and can build confidence you're doing things correctly. Being able to edit code, and effortlessly re-run it to check functionality quickly helps a lot. An interface like that can really slow down the cycles. – tadman Feb 18 '15 at 21:22
  • If you try and read a number and get a non number, it sets the error state of the stream. When the error state is set all reads will fail on all read attempts. You need to reset the error state so it is clear. – Martin York Feb 18 '15 at 21:23

1 Answers1

0

I think you are validating your input incorrectly. Try doing something like this:

#include <iostream>
#include <limits>

int main()
{
    int x;
    std::cin >> x;
    while(std::cin.fail())
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        std::cout << "Bad entry.  Enter a NUMBER: ";
        std::cin >> x;
    }
}

Source

Community
  • 1
  • 1
vdelricco
  • 749
  • 4
  • 15
  • THANK YOU for the simple solution! I tweaked the code to fit in my code and it worked perfectly. If you could explain how it works, it would make it even better! I understand up until numeric_limits. Regardless, thank you for the help! – Art Byra Feb 20 '15 at 19:49