input validation error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: input validation error

  1. #1
    Join Date
    Nov 2010
    Posts
    81

    input validation error

    Hello again,

    I had to write for this assignment and my program works, my professor doesn't care about validating input, however, I do. I can't figure out for the life of me why using an if/else only will validate once. After that, if you keep putting in wrong choices the program goes all to hell. Should I use a while loop somehow? I don't know how to get it to ask indefinitely - or I just can't remember (I think I did this before, last semester.) This isn't towards my grade, just my understanding... I like validating. Makes stuff stupid-proof.

    Code:
    /***************************\
    |	Degree Converter		|
    |	Code By: Ben Brotsker	|
    |	Last Mod: 02/16/11		|
    \***************************/
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    float C_to_F();
    float F_to_C();
    
    int main()
    {
    	char rerun;
    	//program intro
    	cout	<< "This program converts temperatures between Celcius and Farenheit." << endl
    			<< "Would you like to run this program? (Y/N): ";
    	cin		>> rerun;
    	system("cls");
    
    	int choice;
    	float degree = 0;
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    		//--MENU--//
    		cout	<< "DEGREE CONVERTER" << endl
    				<< "------------------------------------------------" 
    				<< endl << endl
    				<< "Which conversion would you like to perform?" << endl
    				<< "1: Farenheit to Celcius" << endl
    				<< "2: Celcius to Farenheit"
    				<< endl << endl
    				<< "Enter your choice: ";
    		cin		>> choice;
    
    		//validate user input
    		if (choice == 1 || choice == 2)
    		{
    			//menu choices
    			switch (choice)
    			{
    				case 1:
    					degree = F_to_C();
    					break;
    				case 2:
    					degree = C_to_F();
    					break;
    				default:
    					cout	<< "Input Error!  Please restart program!" << endl;
    			}
    		}
    		else
    		{			
    			cout	<< "Please re-enter your choice: ";
    			cin		>> choice;
    		}
    
    		//output result
    		cout	<< setprecision(4);
    		if (choice == 1)
    			cout	<< "The temperature in Celcius is " << degree << " degrees." << endl;
    		else
    			cout	<< "The temperature in Farenheit is " << degree << " degrees." << endl;
    		
    		cout	<< "Would you like to covert another temperature? (Y/N): ";
    		cin		>> rerun;
    		system("cls");
    	}
    
    system("cls");
    cout	<< "Have a nice day!" << endl;
    return 0;
    }
    
    
    //--FUNCTION DEFINITIONS--//
    float F_to_C()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Farenheit to be converted: ";
    	cin		>> Fdeg;
    
    	Cdeg = (Fdeg-32)*0.5556;
    	return Cdeg;
    }
    
    float C_to_F()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Celcius to be converted: ";
    	cin		>> Cdeg;
    
    	Fdeg = (Cdeg*1.8)+32;
    
    	return Fdeg;
    }

  2. #2
    Join Date
    Oct 2010
    Posts
    68

    Re: input validation error

    Code:
    /***************************\
    |	Degree Converter		|
    |	Code By: Ben Brotsker	|
    |	Last Mod: 02/16/11		|
    \***************************/
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    float C_to_F();
    float F_to_C();
    
    int main()
    {
    	char rerun;
    	//program intro
    	cout	<< "This program converts temperatures between Celcius and Farenheit." << endl
    			<< "Would you like to run this program? (Y/N): ";
    	cin		>> rerun;
    	system("cls");
    
    	int choice;
    	float degree = 0;
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    		//--MENU--//
    		cout	<< "DEGREE CONVERTER" << endl
    				<< "------------------------------------------------" 
    				<< endl << endl
    				<< "Which conversion would you like to perform?" << endl
    				<< "1: Farenheit to Celcius" << endl
    				<< "2: Celcius to Farenheit"
    				<< endl << endl
    				<< "Enter your choice: ";
    		cin		>> choice;
    
    		//validate user input
    		if (choice == 1 || choice == 2)
    		{
    			//menu choices
    			switch (choice)
    			{
    				case 1:
    					degree = F_to_C();
    					break;
    				case 2:
    					degree = C_to_F();
    					break;
    				default:
    					cout	<< "Input Error!  Please restart program!" << endl;
    			}
    		}
    		else
    		{			
    			cout	<< "Please re-enter your choice: ";
    			cin		>> choice;
    		}
    
    		//output result
    		cout	<< setprecision(4);
    		if (choice == 1)
    			cout	<< "The temperature in Celcius is " << degree << " degrees." << endl;
    		else
    			cout	<< "The temperature in Farenheit is " << degree << " degrees." << endl;
    		
    		cout	<< "Would you like to covert another temperature? (Y/N): ";
    		cin		>> rerun;
    		system("cls");
    	}
    
    system("cls");
    cout	<< "Have a nice day!" << endl;
    return 0;
    }
    
    
    //--FUNCTION DEFINITIONS--//
    float F_to_C()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Farenheit to be converted: ";
    	cin		>> Fdeg;
    
    	Cdeg = (Fdeg-32)*0.5556;
    	return Cdeg;
    }
    
    float C_to_F()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Celcius to be converted: ";
    	cin		>> Cdeg;
    
    	Fdeg = (Cdeg*1.8)+32;
    
    	return Fdeg;
    }

    After your else statement you will always move into your output results section. You need to find a way to bring it back to the menu, or use the choice the user is entering in the else statement to call the correct function.
    Last edited by Austin.Soucy; February 17th, 2011 at 08:18 AM.

  3. #3
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: input validation error

    Here's pseudo code of some various ways to validate input in a console app:

    Code:
    while(program_should_keep_running)
    {
    	ask_for_input
    
    	if(input_is_not_valid)
    	{
    		show_error_message
    	}
    	else
    	{
    		process_input
    	}
    }
    
    -------------------
    
    while(program_should_keep_running)
    {
    	ask_for_input
    
    	if(input_is_not_valid)
    	{
    		show_error_message
    		continue;
    	}
    
    	process_input
    }
    
    ----------
    
    while(program_should_keep_running)
    {
    	do
    	{
    		ask_for_input
    	} while(input_is_not_valid);
    
    	process_input;
    }
    
    ------------
    
    while(program_should_keep_running)
    {
    	get_input
    
    	process_input;
    }
    
    get_input
    {
    	input_is_valid = false;
    	while(!input_is_valid)
    	{
    		ask_for_input;
    
    		if(bad input condition 1) input_is_valid = false;
    		else if(bad input condition 2) input_is_valid = false;
    		else input_is_valid = true;
    	}
    }

  4. #4
    Join Date
    Nov 2010
    Posts
    81

    Re: input validation error

    Would simply copying the menu into the else statement like it is for the if part work? I thought maybe a do-while would solve it, but I can't make that work either.

  5. #5
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: input validation error

    Here's a quick fix to your code that I think will solve your problem (emphases on 'I think'--I didn't test it)

    Code:
    /***************************\
    |	Degree Converter		|
    |	Code By: Ben Brotsker	|
    |	Last Mod: 02/16/11		|
    \***************************/
    
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    float C_to_F();
    float F_to_C();
    
    int main()
    {
    	char rerun;
    	//program intro
    	cout	<< "This program converts temperatures between Celcius and Farenheit." << endl
    			<< "Would you like to run this program? (Y/N): ";
    	cin		>> rerun;
    	system("cls");
    
    	int choice;
    	float degree = 0;
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    		//--MENU--//
    		cout	<< "DEGREE CONVERTER" << endl
    				<< "------------------------------------------------" 
    				<< endl << endl
    				<< "Which conversion would you like to perform?" << endl
    				<< "1: Farenheit to Celcius" << endl
    				<< "2: Celcius to Farenheit"
    				<< endl << endl
    				<< "Enter your choice: ";
    		cin		>> choice;
    
    		//validate user input
    		if (choice == 1 || choice == 2)
    		{
    			//menu choices
    			switch (choice)
    			{
    				case 1:
    					degree = F_to_C();
    					break;
    				case 2:
    					degree = C_to_F();
    					break;
    				default:
    					cout	<< "Input Error!  Please restart program!" << endl;
    			}
    		}
    		else
    		{			
    			cout	<< "Please re-enter your choice: ";
    			// cin		>> choice;
    			continue;
    		}
    
    		//output result
    		cout	<< setprecision(4);
    		if (choice == 1)
    			cout	<< "The temperature in Celcius is " << degree << " degrees." << endl;
    		else
    			cout	<< "The temperature in Farenheit is " << degree << " degrees." << endl;
    		
    		cout	<< "Would you like to covert another temperature? (Y/N): ";
    		cin		>> rerun;
    		system("cls");
    	}
    
    system("cls");
    cout	<< "Have a nice day!" << endl;
    return 0;
    }
    
    
    //--FUNCTION DEFINITIONS--//
    float F_to_C()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Farenheit to be converted: ";
    	cin		>> Fdeg;
    
    	Cdeg = (Fdeg-32)*0.5556;
    	return Cdeg;
    }
    
    float C_to_F()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Celcius to be converted: ";
    	cin		>> Cdeg;
    
    	Fdeg = (Cdeg*1.8)+32;
    
    	return Fdeg;
    }

  6. #6
    Join Date
    Oct 2010
    Posts
    68

    Re: input validation error

    Quote Originally Posted by LogicWavelength View Post
    Would simply copying the menu into the else statement like it is for the if part work?
    Then what happens if the user inputs an invalid choice twice in a row?

    Here is a solution to your problem. This will fix your issue with the least amount of changes.

    Code:
    int main()
    {
    	char rerun;
    	//program intro
    	cout	<< "This program converts temperatures between Celcius and Farenheit." << endl
    			<< "Would you like to run this program? (Y/N): ";
    	cin		>> rerun;
    	system("cls");
    
    	int choice;
    	float degree = 0;
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    		//--MENU--//
    		cout	<< "DEGREE CONVERTER" << endl
    				<< "------------------------------------------------" 
    				<< endl << endl
    				<< "Which conversion would you like to perform?" << endl
    				<< "1: Farenheit to Celcius" << endl
    				<< "2: Celcius to Farenheit"
    				<< endl << endl
    				<< "Enter your choice: ";
    		cin		>> choice;
    
    
            int correctchoice = 0;
    	while (correctchoice == 0)
    	{
    		//validate user input
    		if (choice == 1 || choice == 2)
    		{
    			//menu choices
    			switch (choice)
    			{
    				case 1:
    					degree = F_to_C();
    					correctchoice = 1;    //This is a correct choice, allow user to break out of loop
    					break;
    				case 2:
    					degree = C_to_F();
    					correctchoice = 1;    //This is a correct choice, allow user to break out of loop
    					break;
    				default:
    					cout	<< "Input Error!  Please restart program!" << endl;
    			}
    		}
    		else
    		{			
    			cout	<< "Please re-enter your choice: ";    //Not a correct choice, do not allow user to break out of loop
    			cin		>> choice;
    		}
    	}
    
    		//output result
    		cout	<< setprecision(4);
    		if (choice == 1)
    			cout	<< "The temperature in Celcius is " << degree << " degrees." << endl;
    		else
    			cout	<< "The temperature in Farenheit is " << degree << " degrees." << endl;
    		
    		cout	<< "Would you like to covert another temperature? (Y/N): ";
    		cin		>> rerun;
    		system("cls");
    	}
    
    system("cls");
    cout	<< "Have a nice day!" << endl;
    return 0;
    }
    
    
    //--FUNCTION DEFINITIONS--//
    float F_to_C()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Farenheit to be converted: ";
    	cin		>> Fdeg;
    
    	Cdeg = (Fdeg-32)*0.5556;
    	return Cdeg;
    }
    
    float C_to_F()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Celcius to be converted: ";
    	cin		>> Cdeg;
    
    	Fdeg = (Cdeg*1.8)+32;
    
    	return Fdeg;
    }
    Last edited by Austin.Soucy; February 17th, 2011 at 03:43 PM.

  7. #7
    Join Date
    Nov 2010
    Posts
    81

    Re: input validation error

    That definitely isn't how I solved my validation before. I like that solution though... it's crazy how logical problem solving is such an integral part of programming. I showed my professor earlier and she didn't even care that it didn't indefinitely fix it... her main concern was that I not put any cout statements in my function, so when we learn to write classes I don't make mine confusing.

    EDIT: Why do you leave the if statement then, if the variable correctchoice solves the validation? It needs the "else" part in order to re-ask for choice from user?
    Last edited by LogicWavelength; February 17th, 2011 at 08:22 PM.

  8. #8
    Join Date
    Oct 2010
    Posts
    68

    Re: input validation error

    Quote Originally Posted by LogicWavelength View Post
    EDIT: Why do you leave the if statement then, if the variable correctchoice solves the validation? It needs the "else" part in order to re-ask for choice from user?
    In this case you can think of the 'else' as your Error Handling code. This is what will happen if the user inputs a bad value. Typically I would not ask for the user input again in the else statement and I would structure my loop a little differently. But at the very least you need to inform the user that they made an invalid selection.

    Here is something I might do:
    Code:
    int main()
    {
    	char rerun;
    	//program intro
    	cout	<< "This program converts temperatures between Celcius and Farenheit." << endl
    			<< "Would you like to run this program? (Y/N): ";
    	cin		>> rerun;
    	system("cls");
    
    	int choice;
    	float degree = 0;
    
    	while (rerun == 'y' || rerun == 'Y')
    	{
    
    	int correctchoice = 0;
    	while (correctchoice == 0)
    	{
    				//--MENU--//
    		system("cls");
    		cout	<< "DEGREE CONVERTER" << endl
    				<< "------------------------------------------------" 
    				<< endl << endl
    				<< "Which conversion would you like to perform?" << endl
    				<< "1: Farenheit to Celcius" << endl
    				<< "2: Celcius to Farenheit"
    				<< endl << endl
    				<< "Enter your choice: ";
    		cin		>> choice;
    		//validate user input
    		if (choice == 1 || choice == 2)
    		{
    			//menu choices
    			switch (choice)
    			{
    				case 1:
    					degree = F_to_C();
    					correctchoice = 1;
    					break;
    				case 2:
    					degree = C_to_F();
    					correctchoice = 1;
    					break;
    				default:
    					cout	<< "Input Error!  Please restart program!" << endl;
    			}
    		}
    		else
    		{			
    			cout	<< "Your choice was invalid.";
    			Sleep(500);
    		}
    	}
    
    		//output result
    		cout	<< setprecision(4);
    		if (choice == 1)
    			cout	<< "The temperature in Celcius is " << degree << " degrees." << endl;
    		else
    			cout	<< "The temperature in Farenheit is " << degree << " degrees." << endl;
    		
    		cout	<< "Would you like to covert another temperature? (Y/N): ";
    		cin		>> rerun;
    		system("cls");
    	}
    
    system("cls");
    cout	<< "Have a nice day!" << endl;
    return 0;
    }
    
    
    //--FUNCTION DEFINITIONS--//
    float F_to_C()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Farenheit to be converted: ";
    	cin		>> Fdeg;
    
    	Cdeg = (Fdeg-32)*0.5556;
    	return Cdeg;
    }
    
    float C_to_F()
    {
    	float Fdeg;
    	float Cdeg;
    	cout	<< endl
    			<< "Please enter the degrees in Celcius to be converted: ";
    	cin		>> Cdeg;
    
    	Fdeg = (Cdeg*1.8)+32;
    
    	return Fdeg;
    }

    I think using Sleep() is considered bad form, so there are probably better ways to go about it. But I like the feel it gives to the app.
    Last edited by Austin.Soucy; February 17th, 2011 at 10:30 PM.

  9. #9
    Join Date
    Nov 2010
    Posts
    81

    Re: input validation error

    haha... I haven't had the chance to copy your code in to see how it changes things, but I still don't "see" where it keeps asking if the idiot keeps hitting wrongs keys on purpose. I'm tired and maybe it'll make sense when I see it run... so it just keeps displaying the whole menu when they don't give a valid input?

  10. #10
    Join Date
    Oct 2010
    Posts
    68

    Re: input validation error

    Quote Originally Posted by LogicWavelength View Post
    haha... I haven't had the chance to copy your code in to see how it changes things, but I still don't "see" where it keeps asking if the idiot keeps hitting wrongs keys on purpose. I'm tired and maybe it'll make sense when I see it run... so it just keeps displaying the whole menu when they don't give a valid input?
    Yes. The only way your program can get out of that loop is by changing correctchoice to something other than zero(you could also use bool here). So you should only allow your program to change correctchoice when a correct choice has indeed been entered. This way execution will be stuck inside the loop until a valid option is chosen.

    Also, remember that you still have to do input validation to handle a string or letter. Right now if the user enters a letter/string it will break your app.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center