Hey,
So, in my program I have to take a start time, the length of the call and the rate in order to output it to the user. The only problem I have is validating the input to make sure the start time doesn't go over .59 (HH.MM format) in any instance. I was thinking about just putting a '&& < 0.59' into the if statements, but then I thought it would hurt the first part. Or am I thinking wrong? Here's my code anyway...
#include <iostream>
#include <conio.h>
usingnamespace std;
int main() //Program to calculate how much long-distance calls would be for differing lengths of time and certain starting times
{
float start;
float rate;
int minutes;
//Call start times displayed as HH.MM
cout << "Enter what time the call was started (in HH.MM format): ";
cin >> start;
if (start >= 00.00 && start <= 06.59)
{
rate = 0.05;
}
elseif (start >= 7.00 && start <= 19.00)
{
rate = 0.45;
}
elseif (start >=19.01 && start <= 23.59)
{
rate = 0.20;
}
cout << "\nNow enter how long the call was in minutes: ";
cin >> minutes;
cout << "\nThe charge for this phone call was $" << rate * minutes << ".";
_getch();
return 0;
}
You should not be storing time in numeric types. Numeric types are not appropriate for storing dates or times in a human-readable format. You can store each part of the time separately in numeric types, but what you are doing currently will only cause you more problems.
You could validate it with regex if your compiler supports C++11, or you could just validate it by hand using std::isdigit. After you validate it, then you can just use a std::istringstream to read the number, ignore the period, and read the other number.