How to test input type before storing C++

In summary: How many minutes did it take Jeff to get home today?It took Jeff 5 minutes to get home today.Press 0, Enter to end.
  • #1
foges
53
0
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
 
Technology news on Phys.org
  • #2
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:
#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;
}
 
  • #3
thanks :)
 
  • #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:
#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;
}
 
  • #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:
#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;
}
 
  • #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.
 
  • #7
zabbadahun said:
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.
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.
 
  • #8
I fixed it this is how it was supposed to work
Code:
#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;
}
 

1. How do I test the input type before storing it in C++?

To test the input type before storing it in C++, you can use the typeid operator. This operator returns an object of type type_info, which contains information about the type of the input. You can then use the name() method to retrieve the name of the input type and compare it to the expected type.

2. Why is it important to test the input type before storing it in C++?

Testing the input type before storing it in C++ is important for ensuring that the data is of the correct type and can be properly processed by the program. This can help prevent errors and crashes caused by unexpected input types.

3. What happens if I don't test the input type before storing it in C++?

If you don't test the input type before storing it in C++, there is a risk of storing data that is of the wrong type. This can lead to errors and unexpected behavior in your program, as it may try to process the data in a way that is not appropriate for its type.

4. Can I use conditional statements to test the input type in C++?

Yes, you can use conditional statements such as if-else or switch to test the input type in C++. You can use the typeid operator within the conditional statement to check the type of the input and perform different actions based on the result.

5. Are there any other methods for testing the input type in C++?

Yes, there are other methods for testing the input type in C++. One option is to use templates, which allow for generic programming and can help with type checking. Another option is to use the dynamic_cast operator, which can be used to convert the input to a specific type and check if the conversion was successful.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top