Proper of protecting from bad input. What is the professional way of dealing with it?

I am currently going through professor Stroustrup book: "Programming principles and practice, 2 nd ed.".

It is a task from Chapter 5, task 5 - Write a Celsius to Kelvin (and reverse) calculator, paying attention to bad input and impossible temp values.


Code works, but I wanted to ask you for a code review.
1. Is everything done in a proper way?
2. Is it a proper way of dealing with bad input? How do professionals deal with bad input? (you can give me even some advanced ideas, I will just come back to them later on)



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>
#include <stdexcept>

using namespace std;

inline void error(const string& s)//wrapper provided by prof. Stroustrup
{
	throw runtime_error(s);
}

double ctok(double c)                   
{
	if (c < -273.15)   error("bad argument");        
	return c + 273.15;
}

double ktoc(double k)
{
	if (k < 0) error("bad argument");  
	return k-273.15;
}

int main()
{
        double c, k;
	char menu_choice;                         
                                           
try
{          
	while (true)
	{	
		cout << "From Celsius to Kelvin (K) or reverse (C)?"<< endl;		
		cin >> menu_choice;
		switch (menu_choice) 
		{
		case 'k': 
			cout << "Enter the number (in C): " << '\n'; 
			while (!(cin >> c)) {cin.clear();
		cin.ignore(8, '\n');cout << "Wrong input" << endl;}      
			cout << "In Kelvins it is: " << ctok(c) << '\n'; 
		break;
		case 'c':
			cout << "Enter the number (in K): " << '\n';  
			while (!(cin >> k)) {cin.clear();
		cin.ignore(8, '\n');cout << "Wrong input" << endl;}  
			cout << "In Celsius it is: " << ktoc(k) << '\n'; 
		break;
		default:
			cout << "Invalid choice!"<< endl;
			cin.clear();
			cin.ignore(8, '\n');		
		break;
		}	
		 
	}
}
catch (exception& e)
{
	cerr << "Error: " << e.what() << '\n';
}               
}
Last edited on
Only some smaller things.

I would use better function names like CelciusToKelvin().
Also I would use better error texts like "Celcius must not be smaller than -273.15.
Finally when promping for the input you might give the valid range

Apart from that the code looks ok.
YOu really need to convert the menu_choice to lower case, if you're going for clean input. If the user for whatever reason enters a capital letter, they will get invalid choice. That's not totally professional. try adding this

1
2
3
4
cin >> menu_choice;
menu_choice=tolower(menu_choice);

switch (menu_choice)


Also, while "what" is true?

 
while ((menu_choice!='k') && (menu_choice!='c') && (menu_choice!='q'))


why did I add Q? How does the user end the program? What clue is the user given to exit the program? Professionally speaking - you need to give them a way to voluntarily quit the program without entering something random.

Also, if the menu choices were integer, you could move your cin into the while statement to protect from users entering letters instead of numbers and breaking your program.

1
2
3
4
5
6
7
8
9
10
11
12
while ((!cin>>choice) || (choice < 1) || (choice >5)){
		cout << "\n\n\nThe Rectangle Program\n" << endl;
		cout << "1.  Enter Rectangle length" << endl;
		cout << "2.  Enter Rectangle height" << endl;
		cout << "3.  Calculate Perimeter" << endl;
		cout << "4.  Calculate Area" << endl;
		cout << "5.  Exit\n" << endl;
		cout << "Enter choice:  ";
		cin.clear();
		cin.sync();
		cin >> choice;
	}
Last edited on
Topic archived. No new replies allowed.