How to ignore certain input?

Hi,

I would be grateful if someone could take a look at my code and advise me. I am currently unit testing the menu function to make sure it is working as I intended it to. So far I am reasonably happy but the problem I have is if I enter an invalid value, such as float, my program terminates. Therefore, I was wondering how I ensure that the use enters a valid value?

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
//Author:       
//Date: 	04/10/2013
//Title:        Temperature Scale Convertor
//Description:  User selects the conversion from a menu then inputs value to be converted, converted value is outputted

#include <iostream>
#include <cstdlib>//header to allow program to use clear screen command

using namespace std;

int menu (int &selection);//prototype for menu function

void celsius_input (float &celsius_value);//prototype to get user to input temperature in Celsius
void fahrenheit_input (float &fahrenheit_value);//prototype to get user to input temperature in Fahrenheit

float convert_to_fahrenheit (float celsius_value, float celsius_converted);//prototype for converting from Celsius to Fahrenheit
float convert_to_celsius (float fahrenheit_value, float fahrenheit_converted);//prototype for converting from Fahrenheit to Celsius

void celsius_output (float fahrenheit_converted);//prototype that outputs converted Fahrenheit temperature in Celsius
void fahrenheit_output (float celsius_converted);//prototype that outputs converted Celsius temperature in Fahrenheit

int main ()
{
 	float celsius_value;//declaring variable of type float to hold temperature in Celsius
	float fahrenheit_value;//declaring variable of type float to hold temperature in Fahrenheit
	float celsius_converted;//declaring variable of type float to hold value of temperature in Fahreheit after the Celsius Temperature has been converted
	float fahrenheit_converted;//declaring variable of type float to hold value of temperature in Celsius after the Fahrenheit Temperature has been converted
	int selection;//declaring variable of type int to hold menu choice inputted by user
	
	menu (selection);//calling function to display menu and get user's selection
	cout << "\nSelection value is: " << selection;//used in unit testing menu function
	//if statement to be added once unit testing on menu function completed
	//if statement used to make sure valid selection inputted by user, keeps looping until valid selection made
	/*if (selection == 1 || selection == 2 || selection == 3)
	{
		cout << "";
	}
	else
	{
		system("CLS");//clears screen
		menu (selection);
    }*/

	//the next two lines of code ensure display window remains open
	int x;
	cin >> x;
	return 0;
}

//unit testing conducted 04/10/13
//unit testing - test data used = 1, 2, 3, [arbitrary int value within range: –2147483648 to 2147483647] & arbitrary float values
//function displays menu and user prompted to make a selection. The selection is assigned an integer value which is used to select the corresponding switch statement
int menu (int &selection)
{
	cout << "Menu" << endl;
	cout << "=======" << endl;
	cout << "(1)\tConvert Celsius to Fahrenheit" << endl;
	cout << "(2)\tConvert Fahrenheit to Celsius" << endl;
	cout << "(3)\tExit" << endl;
	cout << "\nEnter choice from menu: ";
	//read input from user
	cin >> selection;//selection variable initialised using value inputted by user
}
Last edited on
Loop your menu until the value is valid.
Thanks Grex.

Unfortunately, when I do that and enter a float value below 1 or >= 4 it creates an infinite loop. Same is true for char & strings.

Basically what I am getting at is I want the programme to ignore, floats, characters, strings and any integer values outwith this range: –2147483648 to 2147483647
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
       cout << "Menu" << endl;
       cout << "=======" << endl;
       cout << "(1)\tConvert Celsius to Fahrenheit" << endl;
       cout << "(2)\tConvert Fahrenheit to Celsius" << endl;
       cout << "(3)\tExit" << endl;
       cout << "\nEnter choice from menu: ";
       //read input from user
       cin >> selection;//selection variable initialised using value inputted by user
       cin.clear();
       cin.ignore(100, '\n');
}while ( selection < 1 || selection > 3);


That might help but you should check this: http://www.cplusplus.com/forum/articles/6046/
'Accept a valid integer from the user within a certain range' seems to be a good candidate for a function of the type that you were thinking of earlier.

Something like this ought to do what you want:

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

// read an integer in the range [ minv, maxv ] from std::cin
int read_integer( int minv, int maxv )
{
    int value ;

    std::cout << "please enter an integer in [ " << minv << ", " << maxv << " ]: " ;

    // if the attempt to read the value is successful, and the value is in range
    if( std::cin >> value && value >= minv && value <= maxv )
        return value ; // we are done, just return the value

    // otherwise an error has occurred

    std::cin.clear() ; // clear the possible error state of the stream
    // http://www.cplusplus.com/reference/ios/ios/clear/

    std::cin.ignore( 1000, '\n' ) ; // discard characters remaining in the input buffer
    // http://www.cplusplus.com/reference/istream/istream/ignore/

    return read_integer( minv, maxv ) ; // try once again
}

int menu()
{
	std::cout << "Menu" << '\n';
	std::cout << "=======" << '\n';
	std::cout << "(1)\tConvert Celsius to Fahrenheit" << '\n';
	std::cout << "(2)\tConvert Fahrenheit to Celsius" << '\n';
	std::cout << "(3)\tExit" << '\n';
	std::cout << "\nEnter choice from menu: \n";

	return read_integer( 1, 3 ) ; //read valid input from user and return it.
}

int main()
{
   int selection = menu() ;
   std::cout << "you selected " << selection << '\n' ;
}
Thanks a lot guys, your help was really appreciated.

I think some of this is just beyond me at the moment but I am sure it will all make more sense as I build up my knowledge.
Topic archived. No new replies allowed.