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.