LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problems with data validation and cin (https://www.linuxquestions.org/questions/programming-9/problems-with-data-validation-and-cin-669928/)

darkangel29 09-14-2008 06:38 PM

Problems with data validation and cin
 
I have the following code
Code:

cout<<"What is the hour of the activity (Standard hour 1-24)?";
      cin>>hour;
while(!(cin>>hour) || hour<0 || hour>24 )
        {
                cout<<"\nThe hour entered is not valid. Please try again: ";
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(),'\n');
        }

I need the user to enter an hour between 1 and 24 and make sure that the data enter is an integer, now I'm able to do this but when the user enter the time for the first time it doesn't want to read it unless is not a number , they have to press enter and enter it one more time for the program to read it. I'm using Microsoft Visual Studio 2005. Thanks

elprawn 09-14-2008 08:14 PM

Try deleting the first cin statement. You have two, one in and one outside the while statement so maybe that's why only the second one works.

darkangel29 09-14-2008 08:23 PM

Thanks it works now. Aleluya!!!!!!!!!!!


All times are GMT -5. The time now is 10:49 AM.