If statement inside of a do while loop to error check user input

I am having some major issues error checking user input inside a do while loop. I want to post the code for it but I'm not sure how to do it on here. If someone could help me with that than we can move on with the help with the actual code :P
Last edited on
copy and paste the code into the text field, but make sure you use the format option for code named 'source code' (those tools should be next to the text field).
There that should do it. It's a mass conversion program that goes from imperial to metric and then back. I have the error checking working just fine for the unit of measurement its checking the int is the issue. Try running this and see what you can come up with.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*	Christopher Reath
	May 11th, 2012
	This program converts a mass unit of measurement into another type of measurement
*/

#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
#if defined(_DEBUG)
	int dbgFlags = ::_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	// Bitwise or. check block integrity on every memory call
	dbgFlags |= _CRTDBG_CHECK_ALWAYS_DF;
	// Don't always remove blocks on delete
	dbgFlags |= _CRTDBG_DELAY_FREE_MEM_DF;
	// Check for memory leak at process termination
	dbgFlags |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(dbgFlags);
#endif

	char answer;
	double mass, convertedMass = 0;
	string unit, input, convertedUnit;
	int done = 0;

	// do while loop for Input validation and convertions
	do
	{
		// Prompt the user for input
		cout << "Enter the mass and unit of measurement you wish to convert\n";
		cout << "(g, kg, lb, oz, tonne, ton, eg. 10ton)\n";
		cout << "No spaces in between the mass and unit: ";
		cin >> input;
		istringstream iss(input);
		iss >> mass >> unit;

		if(!cin.good())
		{
			cout << "You've entered the wrong data type.\n"
				"Please enter a number.";
		}
		cin.clear();
		cin.sync();

		if(unit == "kg")	// Converts from kilograms to pounds
		{
			convertedMass = mass * 2.20462262;
			convertedUnit = "lb";
		}
		else if(unit == "lb")	// Converts from pounds to kilograms
		{
			convertedMass = mass * 0.45359237;
			convertedUnit = "kg";
		}
		else if(unit == "g")	// Converts from grams to ounces
		{
			convertedMass = mass * 0.0352739619;
			convertedUnit = "oz";
		}
		else if(unit == "oz")	// Converts from ounces to grams
		{
			convertedMass = mass * 28.3495231;
			convertedUnit = "g";
		}
		else if(unit == "tonne")	// Converts from tonne to short ton
		{
			convertedMass = mass * 1.10231131;
			convertedUnit = "ton";
		}
		else if(unit == "ton")	// Converts from short ton to tonne
		{
			convertedMass = mass * 0.90718474;
			convertedUnit = "tonne";
		}
		else
		{
			cout << "Not a valid unit of measurement.\n";	// Error check
		}

		// Output the converted mass to the user
		cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl;

		// Ask if you want to do another convertion after already completing one
		cout << "\nReady to do some convertions? (yes or no): ";
		cin >> answer;
		cout << endl;

		if(answer == 'n' || answer == 'N')
		{
			++done;
		}
		
		cin.clear();	// Flush the buffer
		cin.sync();
	
	}while(done !=1);	// End do while loop

	return 0;
}
This is for a project due really soon. If anyone could give me any feedback or tips please do so soon.
Tip: Explain, in detail, the problem you're having to increase your odds of getting a quick response. Supplying examples of input that you wish to handle correctly but don't would've been useful.

Tip: Don't 'bump' your post an hour after you make it.

cin.sync() is not guaranteed to do anything useful.

I would probably change:

1
2
3
4
5
6
7
		if(!cin.good())
		{
			cout << "You've entered the wrong data type.\n"
				"Please enter a number.";
		}
		cin.clear();
		cin.sync();


to look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
		if ( !cin )
		{
			std::cerr << "You've entered the wrong data type.\n"
				"Please enter a number.";

			cin.clear() ;

			// ignore the rest of the offending line:
			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ) ;

			// skip to the loop condition
			continue ;
		}


http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Last edited on
Ok the real problem I am having is when someone enters something like "ukg" into my program (kg being kilograms the unit of measurement. u being someone entering a letter instead of a number for the mass) I want the program to run that if statement and the text that is inside it and return them to the prompt to enter the mass and unit again. As of right now if I do enter "ukg" then the program seems to not even run the if(!cin.good()) statement and go to the end of the if else statement where my else is the error check of my unit of measurement and prints out that cout instead. Also even if the program error checks it still likes to show the program trying to convert whatever it is they entered which outputs a bunch of garbage. If this is not clear enough please tell me what would make it clearer.
And did you try replacing the code I said I would change?
Yes I did and again it just skips right by the error check IF and this time instead of even saying it is an invalid input it just outputs the same conversion I did just before inputing "ukg" so now it doesn't even read it as an error AND also doesn't flush the buffer since I got rid of cin.sync
1
2
3
4
5
6
		if(! mass)
		{
			cout << "You've entered the wrong data type.\n"
				"Please enter a number.";
				continue;
		}
Never mind boys I got it. This seemed to work just fine compared to what I have been trying.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
	char answer;
	double mass, convertedMass = 0;
	string unit, input, convertedUnit;
	int done = 0;

	// do while loop for Input validation and convertions
	do
	{
		// Prompt the user for input
		cout << "Enter the mass and unit of measurement you wish to convert\n";
		cout << "(g, kg, lb, oz, tonne, ton, eg. 10ton)\n";
		cout << "No spaces in between the mass and unit: ";
		cin >> input;
		istringstream iss(input);
		iss >> mass >> unit;

		if(mass > 0) // Error check to confirm the user has inputed a proper value
		{
		if(unit == "kg")	// Converts from kilograms to pounds
		{
			convertedMass = mass * 2.20462262;
			convertedUnit = "lb";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "lb")	// Converts from pounds to kilograms
		{
			convertedMass = mass * 0.45359237;
			convertedUnit = "kg";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "g")	// Converts from grams to ounces
		{
			convertedMass = mass * 0.0352739619;
			convertedUnit = "oz";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "oz")	// Converts from ounces to grams
		{
			convertedMass = mass * 28.3495231;
			convertedUnit = "g";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "tonne")	// Converts from tonne to short ton
		{
			convertedMass = mass * 1.10231131;
			convertedUnit = "ton";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "ton")	// Converts from short ton to tonne
		{
			convertedMass = mass * 0.90718474;
			convertedUnit = "tonne";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else
		{
			cout << "Not a valid unit of measurement.\n";	// Error check
		}

		// Ask if you want to do another convertion after already completing one
		cout << "\nReady to do some convertions? (yes or no): ";
		cin >> answer;
		cout << endl;

		if(answer == 'n' || answer == 'N')
		{
			++done;
		}
		}
		else
		{
			cout << "You have not a number for this convertion\n\n";
		}

		cin.clear();	// Flush the buffer and resets mass so input can be checked
		cin.sync();
		mass = -1;
	
	}while(done !=1);	// End do while loop

	return 0;
} 
Ahh, I didn't look at the code closely enough. The error state would actually occur in iss, not cin (unless you redirected from file, I suppose, and it ran into eof.)

Here's an alternate version, if you should care to actually check error states (important parts in bold):

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <limits>
#include<iostream>
#include<string>
#include<sstream>

using namespace std;

int main()
{
	char answer;
	double mass, convertedMass = 0;
	string unit, input, convertedUnit;
	int done = 0;

	// do while loop for Input validation and convertions
	do
	{
		char const* prompt = "Enter the mass and unit of measurement you wish to convert\n"
		                     "(g, kg, lb, oz, tonne, ton, eg.10ton)\n"
		                     "No spaces in between the mass and unit: " ;

		// Prompt the user for input
		cout << prompt ;
		
		while ( cin >> input )
		{
			istringstream iss(input);

			if ( !(iss >> mass >> unit)  ||  mass <= 0.0 )
				std::cout << "You didn't enter a valid number or didn't supply a unit of measurement.\n" << prompt ;
			else
				break ;
		}

		if(unit == "kg")	// Converts from kilograms to pounds
		{
			convertedMass = mass * 2.20462262;
			convertedUnit = "lb";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "lb")	// Converts from pounds to kilograms
		{
			convertedMass = mass * 0.45359237;
			convertedUnit = "kg";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "g")	// Converts from grams to ounces
		{
			convertedMass = mass * 0.0352739619;
			convertedUnit = "oz";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "oz")	// Converts from ounces to grams
		{
			convertedMass = mass * 28.3495231;
			convertedUnit = "g";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "tonne")	// Converts from tonne to short ton
		{
			convertedMass = mass * 1.10231131;
			convertedUnit = "ton";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else if(unit == "ton")	// Converts from short ton to tonne
		{
			convertedMass = mass * 0.90718474;
			convertedUnit = "tonne";
			cout << mass << unit << " is equal to " << convertedMass << convertedUnit << endl << endl; // Outputs conversion
		}
		else
		{
			cout << "Not a valid unit of measurement.\n";	// Error check
		}

		// Ask if you want to do another convertion after already completing one
		cout << "\nReady to do some convertions? (yes or no): ";
		cin >> answer;
		cout << endl;

		// take care of someone entering more than one letter for answer.
		if ( cin.peek() != '\n' )
			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ) ;

		if(answer == 'n' || answer == 'N')
		{
			++done;
		}
	}while(done !=1);	// End do while loop

	return 0;
}
Topic archived. No new replies allowed.