because I wouldn't even know what to attempt when it came to that part of the program. I cant find any examples or anything online or in my book. ive posted same question on a few forums and cant get a straight answer from anybody.
ive got a problem validating the minutes so that the user can input 00.59 but not 00.60
as the code is right now, the user can input 00.58 but not .59
if I try to tweek the code a little, it will allow 00.59 but will also allow 00.60
can someone help
// long-distance call charge.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
usingnamespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// variables
float startTime, // time at start of call
endTime, // time at end of call
totalCharge, // cost of the call
HH, // hours
MM; // minuts
// constants
constfloat rpm1 = 0.07, // rate per minute 1 (00.00 - 07.59)
rpm2 = 0.12, // rate per minute 2 (08.00 - 15.59)
rpm3 = 0.28, // rate per minute 3 (16.00 - 23.59)
min_time = 00.00, // minumum time
max_time = 23.59, // maximum time
max_min = 0.59; // maximum minutes
// set the numeric output formatting.
cout << fixed << showpoint << setprecision(2);
// get user inputs
cout << "Please enter the start time of the call in HH.MM format using military time. ";
cin >> startTime;
// validate input
if (startTime >= min_time && startTime <= max_time) // validates start time is greater than 00.00 and less than 23.59
MM = fmod(startTime, 1); // extracts minutes from the start time
else
// an invalid time was entered.
cout << "that is an invalid time. Rerun the program and try again. ";
if (startTime < min_time && startTime > max_time) exit(0);
if (MM > max_min)
{
cout << "that is an invalid time. Rerun the program and try again. ";
exit(0);
}
return 0;
}
ive got a problem validating the minutes so that the user can input 00.59 but not 00.60
Simply do not use double or float for time. Time is precise value and by the way it is not decimal but you could not tell system that 00.60 = 01.00
So input it as a string (char array) then remove the dot from string and split it to two strings and convert both manually to integers (with atoi or sscanf). Then multiply first value by 60 and add to second if you need.