I apologize if this has been asked before. I tried googling an answer and came up dry. Also when I used the search box in this forum it took me out of the forum section of the site and into the reference section. Is there another way to search just the forums?
Right up front this is a homework question. I think I have everything in it correct except for one part. The problem has the user entering in a time in the format of HH.MM
Since there is only 59 minutes in an hour I need to limit the input to numbers between 0 and 59.
In my book it says to use num - static_cast<int>(num)
I have no idea where in the code to put this or exactly how it should look. The book says "Assuming num is a floating-point variable, the following expression will give you its fractional part". Would "num" relate to my "start_time" variable and how would I incorporate this into the program?
In my book it says to use num - static_cast<int>(num)
I have no idea where in the code to put this or exactly how it should look. The book says "Assuming num is a floating-point variable, the following expression will give you its fractional part". Would "num" relate to my "start_time" variable and how would I incorporate this into the program?
Do you understand what the static_cast<int>(num) is actually doing?
Okay, that static cast is converting a floating point number to an int which truncates the fractional part. So what you end up with is the whole part of the floating point number.
So if your number is 5.67 your equation would equivalent to 5.67 - 5 which equals .67.
Also I myself would consider changing the way you're getting the starting value. I would use a string to retrieve this value from the user then use a stringstream to parse the string, after I did some initial validation.
1 2 3 4 5 6 7 8 9 10 11 12 13
string input_value;
cout << "Please enter what time your call started\n";
cout << "Please enter in the form of HH:MM" << endl; // Note the colon instead of the decimal point.
cin >> input_value;
int start_hours, start_min;
char colon;
if(input_value.find(':') == std::string::npos || input_value.length() != 5)
{
// Report error and fix the issue or abort program.
}
istringstream sin(input_value);
sin >> start_hours >> colon >> start_min;
You would assign this value (multiplied by 100) to an int that would give you the start minutes that you would use throughout the rest to the program. In other words convert the floating point number into two integers one integer represents the start hours the other representing the start minutes.