CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2011
    Posts
    15

    Infinite loop help

    Ok, so I've been trying to finish this program, but when I do things a certain way, I go through an infinite loop in which I am not sure how to stop or keep it from happening. The object of the program is to convert feet/inches into centimeters, provide exceptions on the datatypes so that a not numeric number or a negative number can be entered, then keep asking the user to input datatypes until they choose the option to close the program.

    My problem happens ONLY when I enter a negative number in the sequence BEFORE I enter a character exception, this is where is throws me in the loop. But I do not get thrown in the loop if I enter the character first and then the negative number and then the normal numbers to close.

    Can anyone identify the problem here? I've been reading the chapter in this book and trying to understand the examples but I see no fault in their explaining and my coding in comparison. I am sure its a user error and I over looked something.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double feet = 0, inches = 0, centimeters;
        string input = "ERROR: Input stream in fail state";
        bool done = false;
        
                cout << "Use this program to convert Feet and Inches into Centimeters." << endl; //Heading to explain usefulness of the program
                cout << endl;
    
    do //Does the following code until done = true when input is correct.
    {        
        try
        {
                
                cout << "Enter amount of feet: "; //Asks for input of feet
                cin >> feet; //User enters value to store in feet
                cout << "Enter amount of inches: "; //Asks for input of inches
                cin >> inches; //User enters value to store in inches
                cout << endl;
                cout << "Your current values are: " << feet << " feet, " << inches << " inches." << endl; //Outputs feet and inches
                cout << endl;
                
                //throws exceptions to be caught and errors to be found if any
                if(inches < 0)
                        throw inches;
                else if (feet < 0)
                        throw feet;
                else if (!cin)
                        throw input;
        
                //Calculation to convert Feet/Inches into Centimeters
                inches = inches * 2.54; //One inch equals 2.54 centimeters; Multiplies user input by centimeter value.
                feet = feet * 30.48; //One foot = 30.48 centimeters; Multiplies user input by centimeter value.
                centimeters = feet + inches; //Adds the centimeter values together and stores in centimeters
                
                cout << "Here is the conversion in centimeters: " << centimeters << " centimeters" << endl; //Output centimeters
                cout << endl;
    
                done = true;
                
        }//End try
                    
        catch(double x)
        {
                  cout << "You have entered a negative number. ERROR: "<< x << endl;           
        }//End feet catch
        catch(string invalidInput)
        {
                  cout << invalidInput << endl;
                  cin.clear();
                  cin.ignore(100,'\n');
        }//End string catch
    }//End do
    
    while(!done);
        
        system("pause");
        return 0;
    };//End Main
    If you run the code, enter a negative in the first value and then a character, you will get thrown in the loop. If you run the character first and then the negative, no loop.. I'm so confused.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Infinite loop help

    So your algorithm has a flaw, find some way to patch it.

    Why don't you validate both inputs one by one, instead of waiting for both?

    Code:
                cout << "Enter amount of feet: "; //Asks for input of feet
                cin >> feet; //User enters value to store in feet
                if (feet < 0)
                    throw feet;
                else if (!cin)
                    throw input;
    
                cout << "Enter amount of inches: "; //Asks for input of inches
                cin >> inches; //User enters value to store in inches
                cout << endl;
                if(inches < 0)
                    throw inches;
                else if (!cin)
                    throw input;
    
                cout << "Your current values are: " << feet << " feet, " << inches << " inches." << endl; //Outputs feet and inches
                cout << endl;
    
                //throws exceptions to be caught and errors to be found if any
    Another solution would be to clean your stream in the catch, if it is not valid:
    Code:
            catch(double x)
            {
                cout << "You have entered a negative number. ERROR: "<< x << endl;
                if(!cin)
                {
                  cin.clear();
                  cin.ignore(-1,'\n');
                }
            }//End feet catch
            catch(string invalidInput)
            {
                cout << invalidInput << endl;
                cin.clear();
                cin.ignore(-1,'\n');
            }//End string catch
    Note: "-1" is unsigned magic meaning "maximum"

    Finally, and perhaps the best solution in this case, is to drop exceptions, and do standard input validation: http://www.parashift.com/c++-faq-lit....html#faq-15.3
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured