Input validation problem

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.

Any help would be greatly appreciated.

Here are my structures that I am using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//Constant for MAXSHUTTLESPEED
const int MAXSHUTTLESPEED = 670616629;

//----------------------------------------------------------------------------------
//STRUCTS	   STRUCTS		STRUCTS		STRUCTS		STRUCTS		STRUCTS		STRUCTS
//----------------------------------------------------------------------------------
struct Planets
{
	string planetName;
	int distanceFromSun;
	double surfaceGravityFactor;
} planets_t[8];

struct Shuttle
{
	char cargoType[256]; //Create a character array able to store 256 characters for cargoType
	int pickupPlanet;
	int deliveryPlanet;
	double shuttleSpeed;    
	double totalDistance;
	double pickupPounds;
	double deliveryPounds;
	double earthPounds;
	double minutes;
	double days;
	double years;

}delivery;
//-------------------------------------------------------------------------------------- 


----Here is my main function::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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;
}


--Here is my gather input function::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//-----------------------------------------------------
//gatherInput: This function gathers and stores input
//provided by the user
//-----------------------------------------------------
void gatherInput ()
{
	//Gather type of cargo
	cout << "//Type of cargo (max 256 characters): ";
	cin.getline (delivery.cargoType, 256);
	
	/*for (int i = 0; delivery.cargoType[i] != '\0'; i++)
		cout << delivery.cargoType[i];*/

	//Gather the pickup planet
	cout << "\n//Pickup and Delivery Planet Menu System:\n";
	cout << "1) Mercury \n2)Venus \n3)Earth \n4)Mars \n5)Jupiter \n6)Saturn \n7)Uranus \n8)Neptune \n\n";
	cout << "//Pickup planet: ";
	cin >> delivery.pickupPlanet;

	//Gather weight of the cargo in	pickup planet pounds
	cout << "//Weight of cargo upon pickup: ";
	cin >> delivery.pickupPounds;
	
	//Gather the delivery planet
	cout << "\n//Pickup and Delivery Planet Menu System:\n";
	cout << "1) Mercury \n2)Venus \n3)Earth \n4)Mars \n5)Jupiter \n6)Saturn \n7)Uranus \n8)Neptune \n\n";
	cout << "//Delivery planet: ";
	cin >> delivery.deliveryPlanet;

	//Gather shuttle speed
	cout << endl << "//Shuttle speed in MPH: ";
	cin >> delivery.shuttleSpeed;

	//Calls inputValidation function to ensure all inputs are appropriate
	inputValidation ();
}


----Here is my input validation function::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//-----------------------------------------------------
//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;
		}
	}
}

Last edited on
Corrected duplicate loop at line 16.
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.
Last edited on
Thank you! I was thinking of this is the wrong way. Appreciate your input.
Topic archived. No new replies allowed.