Validating a float

Hey everyone.

I have an assignment for class where I ask the user to input the start time of a phone call as a float in 24hr format (HH.MM). I need to validate the HH and the MM so i can restart the program from the beginning if they were to put an invalid character or integer for either. example if they were to input 25.10 (no 25th hour) or 23.76 (no 76th minute) i will have to stop them there.

Now I am not sure if I should create a char variable for both HH and MM or create one float for the whole number and be able to validate it as a float?

any assistance would be awesome. thanks!!

Danny
It would probably be easier to just read each number separately as an integer and check them.
Yeah, just
1
2
3
cin >> hour;
cin.ignore();
cin >> min;


Or something.
But if the user is to enter it all in one entry, will what you proposed work wasabi?

Would that take the hours from before the decimal and the minutes from after the decimal if they only have one input of 01.30?
I see three obvious solutions here.
1) take the input in as two separate integers (like Zhuge proposed), this is by far the easiest for you to program.
2) take the input in as a string, then convert it into two separate integers using sub strings.
3) take the input in as a float like you originally thought, and when you want to check for the minutes you could use some shifting in your algorithm to isolate HH and MM.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float timeFl;
int modInt;
int mins;
int hours;
do
{
    cout << "Please input hours and minutes [HH.MM]: "
    cin >> timeFl;

    hours = (int) ( timeFL )             // Gets hours by dropping .MM

    modInt = (int) ( timeFl * 100 );     // Converts HH.MM to HHMM
    mins = modInt%100;                   // Gets minutes by dropping the HH


    if( mins >= 60 || hours >= 24 )
    {
         cout << "Invalid input, time must be in the correct format [hour/minute].\n\n";
    }
}
while( mins >= 60 || hours >= 24 )


Personally I would do option 2 since this code would crash with characters, but option 3 could definitely work. You could always do some combination of 2 and 3 if you care about that error checking.
It sounds like your teacher wants you to do option 3, if so, you should probably do option 3.
PS - I didn't try compiling the code.
Last edited on
I am not aware of any locales where hours and minutes are separated by a full-stop.
Use the punctuation your users expect: HH:MM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
  {
  unsigned hours;
  unsigned minutes;

  while (true)
    {
    cout << "Please enter hours and minutes as HH:MM  " << flush;
    cin >> hours;
    char c;
    cin >> c;
    cin >> minutes;

    if (!cin.good())
      {
      hours = 70;
      cin.clear();
      }
    cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

    if ((hours < 24) && (minutes < 60))
      break;

    cout << "Hey, that is not a valid input. ";
    }

  cout << "Good job! You entered " << hours << " hours and " << minutes << " minutes.\n";
  return 0;
  }

This won't stop the user from entering something like "12.30z" though... for that you'll need to use method 2 as Yggdrasil suggests above. For more, see here:
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827

Hope this helps.
Topic archived. No new replies allowed.