Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to test input type before storing C++

  1. Dec 30, 2007 #1
    Im sure this is a very basic question. Anyways I need to test the input going to my c++ program before cin stores it. (ive basically written a very simple 4 function calculator program and i just want to make sure that my program does not fail if someone were to enter for example a letter instead of a number). how would i do this?

    Thanks
     
  2. jcsd
  3. Dec 30, 2007 #2

    jtbell

    User Avatar

    Staff: Mentor

    Basically, you have to try to read, then test to see if the input succeeded, and act accordingly. Here's an example that illustrates a common technique. If the comments aren't enough, ask away! :smile:

    Code (Text):

    #include <iostream>

    using namespace std;

    int main ()
    {
        cout << "Please enter a number followed by a <return> or <enter>: ";
        double number;
        while ( ! (cin >> number))
        {
            // Enter this loop if input fails because of invalid data.

            cout << "Hey dummy, I said enter a number!  Try again: ";
            cin.clear ();   // reset the "failure" flag

            // The input "cursor" is still positioned at the beginning of
            // the invalid input, so we need to skip past it.

            cin.ignore (1000, '\n');  // Skip to next newline or 1000 chars,
                                      // whichever comes first.  This is why
                                      // you have to follow the input with
                                      // <return> or <enter>.
        }
        cout << "OK, you entered " << number << "." << endl;
        return 0;
    }
     
     
  4. Jan 1, 2008 #3
    thanks :)
     
  5. Mar 31, 2011 #4
    Hey, this is my first time writing anything in c++. I happened across this post when I was working so decided I was going to try it. I first applied this to the cin >> minutes and got that to work. I then attempted to apply it to the cin >> end and that is where I encountered the current problems I have been as of yet unable to remedy. If minutes is entered with characters will print "Please enter the amount of minutes." with the cursor beside it instead of moving to a new line and will allow further input which it will put on a new line but if a number is entered afterwards it will not accept it, this works in debug. If end is entered as characters the program loops printing "press 0, Enter to end." if entered as an integer it functions correctly prompting to "press 0, Enter to end." and ends with 0, Enter. In debug the reverse occurs.

    Code (Text):
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main()
    {
        cout << "How many minutes did it take to get home today Jeff?\n";
    double minutes;
        while ( ! (cin >> minutes))
        {                          
            cout << "Please enter the amount of minutes.\n";
            cin.clear();
            cin.ignore(1000, '\n');
        }
        cout << "It took you " << minutes << " minutes to get home!\n";
        cout << "Press 0, Enter to end\n";
    double end;
        while ( ! (cin >> end))
        {                      
              cout << "That is not a number.\n";
              cin.clear();
              cin.ignore(1000, '\n');
        }
        do
        {
         cout << "press 0, Enter to end.\n";
        }while (end != 0);
        return EXIT_SUCCESS;
    }
     
     
  6. Mar 31, 2011 #5
    Not sure why you wanted two sequential while loops. And that last while loop will just cause an infinite loop if end != 0 since it gathers no input.

    Is this sort of what you're trying to do?

    Code (Text):

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main()
    {
        double minutes = 0.0;
        do {
            cout << "How many minutes did it take to get home today Jeff?\n";
            cout << "press 0, Enter to end.\n";
            while ( ! (cin >> minutes))
            {
                cout << "Please enter the amount of minutes.\n";
                cin.clear();
                cin.ignore(1000, '\n');
            }
            if (minutes != 0.0) {
                cout << "It took you " << minutes << " minutes to get home!\n";
            }
        } while (minutes != 0.0);

        return EXIT_SUCCESS;
    }
     
     
  7. Mar 31, 2011 #6
    I wanted the program to take the value for minute, check if it is an number, prompt to enter a number if not, and then have that value reported. The second part was I did not want the program to simply close I wanted it to be interrupted. I have it prompt "press 0, enter to end" I then wanted it to check if that was a number if not prompt the user to enter a number if the number was not 0 it would simply reiterate to press 0, enter.
     
  8. Mar 31, 2011 #7
    That's pretty much how the code I posted works, if I understand you correctly. Only difference is that you can exit with 0 right off the start (and are told so) and the same prompt is given for every iteration. Can't see how that could possibly be objectionable behaviour, but I could be wrong.
     
  9. Mar 31, 2011 #8
    I fixed it this is how it was supposed to work
    Code (Text):

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main()
    {
        cout << "How many minutes did it take to get home today Jeff?\n";
    double minutes;
        while ( ! (cin >> minutes))
        {                          
            cout << "Please enter the amount of minutes.\n";
            cin.clear();
            cin.ignore(1000, '\n');
        }
        cout << "It took you " << minutes << " minutes to get home!\n";
    double end;
            do
        {
         cout << "press 0, Enter to end.\n";
         while ( ! (cin >> end))
        {                      
              cout << "That is not a number.\n";
              cin.clear();
              cin.ignore(1000, '\n');
        }
        }while (end != 0);
        return EXIT_SUCCESS;
    }
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?