Reputation:
I'm simply trying to let the user enter their race times (in minutes) at each checkpoint. When I try to run in the console, it skips over all the input from user except for the name.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main(void)
{
int RacerName;
int CheckpointOne;
int CheckpointTwo;
int CheckpointThree;
int CheckpointFour;
cout << "Enter the racer's first name: ";
cin >> RacerName;
cout << "Enter the time (in minutes) at checkpoint 1: ";
cin >> CheckpointOne;
cout << "\nEnter the time (in minutes) at checkpoint 2: ";
cin >> CheckpointTwo;
cout << "\nEnter the time (in minutes) at checkpoint 3: ";
cin >> CheckpointThree;
cout << "\nEnter the time (in minutes) at checkpoint 4: ";
cin >> CheckpointFour;
return 0;
}
Upvotes: 0
Views: 187
Reputation: 15872
Barmar corrected your problem, but the over-arching problem you have is that you are not checking to make sure your input was successful. You can do so by modifying your code slightly:
#include <iostream>
#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;
}
Note the 2 utility functions. They both will check to make sure the input is read in correctly. If the input fails, it will clear the error flags and ignore the rest of the line so it can try again.
Upvotes: 0
Reputation: 781210
RacerName
should be a string
, not int
.
string RacerName;
When you type a non-integer in response to that prompt, the conversion fails. The same thing happens with all the other cin
lines, because it's leaving the name that you typed in the input buffer, and each of them is trying to convert it to a number.
Upvotes: 2