Trouble with Input Validation

I am completely new to C++ and am very confused... I just wrote this, and when I enter letters instead of numbers the program crashes. What can I do to fix this so that if letters are entered, it will display the invalid message? Thanks!

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

#include <iostream>
using namespace std;

int main ( )
{
	// Solicit input from the user
	cout << "Enter temperature in Fahrenheit: ";
	int temp;		// Variable holding user input temperature
	cin >> temp;

	// Display the results according to user input
	if (temp <= -362)
	{
		cout << "\nOxygen, Ethyl alcohol, Mercury and water will ";
		cout << "freeze at this temperature." << endl;
	}
	else if (temp >= -306 && temp <= -173)
	{
		cout << "\nEthyl alcohol, Mercury and water will freeze ";
		cout << "and Oxygen will boil at this temperature." << endl;
	}
	else if (temp > -173 && temp <= -38)
	{
		cout << "\nMercury and water will freeze ";
		cout << "and Oxygen will boil at this temperature." << endl;
	}
	else if (temp > -38 && temp <= 32)
	{
		cout << "\nWater will freeze and Oxygen will boil ";
		cout << "at this temperature." << endl;
	}
	else if (temp > 32 && temp < 172)
	{
		cout << "\nOxygen will boil at this temperature." << endl;
	}
	else if (temp >= 172 && temp < 212)
	{
		cout << "\nOxygen and Ethyl alcohol will boil ";
		cout << "at this temperature." <<endl;
	}
	else if (temp >= 212 && temp < 676)
	{
		cout << "\nOxygen, Ethyl alcohol and water will boil ";
		cout << "at this temperature." << endl;
	}
	else if (temp >= 676)
	{
		cout << "\nOxygen, Ethyl alcohol, water and Mercury ";
		cout << "will boil at this temperature." << endl;
	}
	else 
		cout << "\nYou have entered an invalid value." << endl;
	
	// Hold the result on screen for the user to read.
	cin.ignore( );
	cout << "\nPress Enter to exit the program.";
	cin.get( );

	//return 0;
}
You could use <string>instead <int>.
But you should make the if stetmanets using this "".

1
2
3
4
5
6
7
for example:
string temp;
if (temp <= "-362")//here you mean that it is string.
	{
		cout << "\nOxygen, Ethyl alcohol, Mercury and water will ";
		cout << "freeze at this temperature." << endl;
	}


So now your program whatever you put will take it as string,so will not crash
Last edited on
Thanks. I tried using string instead. The program does not crash any more but it treats the letters and symbols entered as numbers, and are showing the results as if I entered a numeric value, not the "invalid value" message. It's so puzzling!
A good way to do it is to read input into a string, then convert that string to the intended type with a stringstream, repeating until the user enters valid input. The example below uses a template parameter to allow it to work with all basic types (and then some).

templates: http://www.cplusplus.com/doc/tutorial/templates/
stringstreams: http://www.cplusplus.com/reference/iostream/stringstream/

This might be overkill, but it'll do the job (Note: I'm not near a compiler, but this should work):

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
#include <iostream>
#include <sstream>
using namespace std;

template<class value_type>		//template allows it to work with all basic types
value_type get_value(string message)
{
	value_type output;
	string input;

	while(true) {

		cout << message;
		getline(cin, input);		//read input as a string until user presses ENTER/RETURN
		
		if(cin) {		//if you read a string successfully...
		
			stringstream extract(input);		//convert input to a new stream...

			if(extract >> output		//if the stream successfully converts to the intended type
				&& !(extract >> input)) 	//and no input is left in the stream
					return output;
		}
		else cin.clear();
		cerr << "\nInvalid input. Please try again.\n"; 	//you didn't enter a valid value if you get here
	}
}

int main()
{
	int i = get_value<int>("Please enter an integer: ");	//Examples: notice the type name in <>'s for each function call
	double d = get_value<double>("Please enter a double: ");
	string s = get_value<string>("Please enter a string: ");
}


If you need to check for ranges, this function can easily be modified to do so...
Topic archived. No new replies allowed.