Hello all,
I have been working on this project for a while now and am having trouble with my input validation function.
---PROBLEM::
My input validation needs to ensure that the user does not input anything other than a number, and that the pickup and delivery planets are not the same when a user enters a number for a pickup planet and a delivery planet.
I have been trying to implement this input validation using the isdigit function, but so far haven't been able to get it exactly right.
int main()
{
bool run_Again;
do
{
cout << "\n//----------------------------------------------------------------------\n";
cout << "//Interplanetary delivery service log system\n";
cout << "//This program will store all data relative to deliveries in question\n";
cout << "//----------------------------------------------------------------------\n";
//Calls writePlanetStruct function
writePlanetStruct ();
//Calls gatherInput function
gatherInput ();
//Calls runProgramAgain function, sets run_Again bool equal to return value
run_Again = runProgramAgain();
//Display message to indicate program exit
if (!run_Again)
cout << "//Exiting the program.\n";
}while (run_Again); //If "NO" is detected, the program will exit, anything else will repeat program operations
system ("pause");
return 0;
}
//-----------------------------------------------------
//inputValidation: This function validates that user
//provided inputs of the proper format
//-----------------------------------------------------
void inputValidation ()
{
//Loop to validate that deliveryPlanet is NOT the same as the pickupPlanet
while (delivery.pickupPlanet == delivery.deliveryPlanet)
{
cout << "//Invalid delivery planet indicated..." << endl << "//You cannot pickup and deliver to same planet.\n";
cout << "//Please re-enter your desired delivery planet: ";
cin >> delivery.deliveryPlanet;
}
//Loop to validate that pickupPounds contains only numeric values
if ((!isdigit(delivery.pickupPounds)) && (!ispunct(delivery.pickupPounds)))
{
while ((!isdigit(delivery.pickupPounds)) && (!ispunct(delivery.pickupPounds)))
{
cout << "//Invalid entry for pickup weight... \n//Please use only numbers to indicate weight: " << endl;
cin >> delivery.pickupPounds;
}
}
//Loop to validate shuttleSpeed is above 0 MPH and less then 670616629 MPH
if ((delivery.shuttleSpeed > MAXSHUTTLESPEED) || (delivery.shuttleSpeed <= 0))
{
while (delivery.shuttleSpeed > MAXSHUTTLESPEED)
{
cout << "//Shuttle speed cannot exceed speed of light... \n//Re-enter shuttle speed in MPH: ";
cin >> delivery.shuttleSpeed;
}
while (delivery.shuttleSpeed <= 0)
{
cout << "//Shuttle must be moving... \n//Re-enter shuttle speed in MPH: ";
cin >> delivery.shuttleSpeed;
}
}
}
All your Shuttle members are numerical, so cin will convert whatever the user enters into a number, even if the user enters non-digits. If you want to validate input, you should read in each value as a string first, and then test each character of the string with isdigit.