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?
//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
usingnamespace 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
}
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
'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.
#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' ;
}