Jaeden Harris February 2016

Cin won't let user enter anything in console before moving on

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;
}

Answers


Barmar February 2016

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.

DEMO


Zac Howland February 2016

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 <<  

Post Status

Asked in February 2016
Viewed 3,028 times
Voted 7
Answered 2 times

Search




Leave an answer