#include #include #include #include #include using namespace std; /* This program will compute pace based on distance and time. */ bool isValidDistance( string, double & ); int getTimePart( string ); int main() { double distance; //for reading in the distance int paceMinutes; //for the pace calculation int paceSeconds; //for the pace calculation int distanceTenths; //the distance in tenths as an int int hours; //this will be the time in hours int seconds; //this will be the time in seconds int minutes; //this is the time in minutes int totalTime; //the total time computed as seconds int secondMile; //the seconds per mile string distanceStr; //used for reading in the distance string hoursStr; //used for reading in the hours string minStr; //used for reading in the minutes string secStr; //used for reading in the seconds //prompt the user for their distance //if they enter bogus info, then we will prompt again do { cout << "Tell me how far you ran: "; getline( cin , distanceStr ); // read in the distance if ( isValidDistance( distanceStr, distance ) ) distanceTenths = distance * 10; //compute the distance in terms of tenths }while( distance == 0 ); //keep looping if the distance is still 0 //set total time to 0 totalTime = 0; while ( totalTime == 0 )//while total time is 0 loop { cout << endl << endl << "How long did it take you?"; cout << endl << "Tell me your hours: "; cin.ignore( 200, '\n' ); getline( cin, hoursStr ); //read the hours hours = getTimePart( hoursStr ); //get the minutes cout << endl << "Tell me your minutes: "; getline( cin, minStr ); //repeat logic from above, but now checking minutes minutes = getTimePart( minStr ); //get the seconds cout << "Tell me your seconds: "; getline( cin, secStr ); //repeat the logic again to check for valid input seconds = getTimePart( secStr ); //compute total time //if the user entered bad input this will be 0 //if the use just pressed enter three times this will be 0 //in both case total time will be 0 and the loop will go again. totalTime = ( hours * 3600 ) + ( minutes * 60 ) + seconds; } //now compute how many seconds per mile secondMile = 10 * ( (double)totalTime / distanceTenths ); //compute pace minutes paceMinutes = secondMile / 60; //compute pace seconds paceSeconds = secondMile % 60; cout << endl << endl; //print out the pace cout << "Your pace is: " << paceMinutes << ":" << setw(2) << setfill('0') << paceSeconds << setfill( ' ' ) << endl; return 0; } bool isValidDistance( string distanceStr, double &distance ) { int counter = 0; bool goodInput = true; //loop through the input while( counter < distanceStr.length() && goodInput) { //if the character we are looking at is a digit or a period if ( isdigit( distanceStr[counter] ) || distanceStr[counter] == '.' ) goodInput = true; //the input is good else goodInput = false; //the input is bad counter++;//increment the counter so we can look at the next character } //check to see if the input was good if ( goodInput ) { istringstream iss(distanceStr); iss >> distance; //get the distance out of the string } else distance = 0;//reset distance to 0 so the loop will continue return goodInput; } int getTimePart( string timePartStr ) { int timePart; if ( timePartStr == "" ) //if they just press enter timePart = 0; //set hours to 0 else { //otherwise check for valid input a la the loop above int counter = 0; bool goodInput = true; while( counter < timePartStr.length() && goodInput) { if ( isdigit( timePartStr[counter] ) ) goodInput = true; else goodInput = false; counter++; } if ( goodInput ) //if the input was good { istringstream iss(timePartStr); iss >> timePart; //grab the hours out } else timePart = 0; //else set the hours to 0 } return timePart; }