#include <iostream>
#include <istream>
#include <limits>
#include <string>
// These 2 functions will read a string/integer from an istream with error checking
std::string ReadString(std::istream& is)
{
std::string result = "";
while (!std::getline(is, result)) // do this until the user enters valid input
{
std::cin.clear(); // clear the error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the invalid data
}
return result;
}
int ReadInt(std::istream& is)
{
int result = -1;
while (!(is >> result))
{
std::cin.clear(); // clear the error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore the invalid data
}
return result;
}
int main(void)
{
std::cout << "Enter the racer's first name: ";
std::string RacerName = ReadString(std::cin); // NOTE: should be a string
std::cout << "Enter the time (in minutes) at checkpoint 1: ";
int CheckpointOne = ReadInt(std::cin);
std::cout << "\nEnter the time (in minutes) at checkpoint 2: ";
int CheckpointTwo = ReadInt(std::cin);
std::cout << "\nEnter the time (in minutes) at checkpoint 3: ";
int CheckpointThree = ReadInt(std::cin);
std::cout << "\nEnter the time (in minutes) at checkpoint 4: ";
int CheckpointFour = ReadInt(std::cin);
std::cout << "\nTimes for " << RacerName << std::endl
<< "\tCheckpoint 1: " << CheckpointOne << std::endl
<< "\tCheckpoint 2: " << CheckpointTwo << std::endl
<< "\tCheckpoint 3: " << CheckpointThree << std::endl
<< "\tCheckpoint 4: " << CheckpointFour << std::endl;
return 0;
}