Like Tree1Likes
  • 1 Post By grumpy

Problem with while loop

This is a discussion on Problem with while loop within the C++ Programming forums, part of the General Programming Boards category; Hi, guys. Need a bit of help. I have a program with try/catch block. It works fine till I press ...

  1. #1
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20

    Problem with while loop

    Hi, guys. Need a bit of help. I have a program with try/catch block. It works fine till I press 'q' to quit program, then it gets into an infinite loop. Can anybody advise what I am doing wrong?
    Code:
                #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    const double CM_PER_INCH = 2.54;
    const int INCHES_PER_FOOT = 12;
    int main()
    {
      int feet, inches, totalInches;
      double centimeter;
      char option;
      string nonDig = "You need to enter numeric digits please.";
      while (option!='q')
      {
      try
      {
        cout << "Please enter the length in feet and inches or enter 'q' to quit the program: ";
        cin >> feet >> inches;
        cout << endl;
        if (feet < 0 || inches < 0)
          throw string ("Negative number");
        else if (!cin)
          throw nonDig;
        totalInches = INCHES_PER_FOOT * feet + inches;
        centimeter = CM_PER_INCH * totalInches;
        cout << "The number in centimeters is: " << centimeter << "cm." << endl;
      }
      catch (int x)
      {
        cout << "Please enter other set of numbers." << endl;
      }
      catch (string s)
      {
          cout << s << endl;
      }
      catch (...)
      {
          cout << "Error in program. Try again" << endl;
      }
      }
      return 0;
    }

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    108
    1. You don't have any instruction to input option. The compiler gives you a hint what might be wrong
    warning: 'option' may be used uninitialized in this function
    but you ignore it.
    2. Check if cin was successful first, then validate entered values.

  3. #3
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    OK, I changed the code as follows:
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    
    using namespace std;
    
    
    const double CM_PER_INCH = 2.54;
    const int INCHES_PER_FOOT = 12;
    
    
    int main()
    {
      int feet, inches, totalInches;
      double centimeter;
      string nonDig = "You need to enter numeric digits.";
      bool done = false;
    
    
      while (!done)
      {
      try
      {
        cout << "Enter length in feet: ";
        cin >> feet;
        cout << "Enter length in inches: ";
        cin >> inches;
        cout << endl;
    
    
        if (feet < 0 || inches < 0)
          throw string ("Negative number. Enter positive number");
        else if (!cin)
          throw nonDig;
    
    
        done = true;
        totalInches = INCHES_PER_FOOT * feet + inches;
        centimeter = CM_PER_INCH * totalInches;
    
    
        cout << "The number in centimeters is: " << centimeter << "cm." << endl;
      }
      catch (int x)
      {
        cout << "Please enter other set of numbers." << endl;
      }
      catch (string s)
      {
          cout << s << endl;
      }
      catch (...)
      {
          cout << "Error in program. Try again" << endl;
      }
      }
      return 0;
    }
    Now it goes into infinite loop when I enter characters instead of digits. What is wrong now?
    Thank you

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    The code is only attempting to read numeric values. When the relevant statements ("cin >> feet" and "cin >> inches") encounter non-numeric input, they will stop and leave the offending non-numeric input in the stream - to be encountered next time reading input is attempted, so the process will repeat forever. The stream is also placed into an error condition, so your code (as written) throws an exception, but immediately catches it and continues.

    What you need to do is read the input as characters (or as a set of characters i.e. a string). You can then examine the input to see whether the input is numeric or non-numeric.



    Unrelated to your problem, your usage of exceptions is ridiculous. Exceptions are not intended for loop control. They are intended for circumstances where a function that detects an error cannot recover from it, not for passing control within a function.
    King Mir likes this.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    just started learning
    Join Date
    Oct 2012
    Location
    Dardanelle, Arkansas, United States
    Posts
    20
    Quote Originally Posted by grumpy View Post

    Unrelated to your problem, your usage of exceptions is ridiculous. Exceptions are not intended for loop control. They are intended for circumstances where a function that detects an error cannot recover from it, not for passing control within a function.
    Well, at least you lived to your username. Did it make your day adding nasty note at the end? I didn't thought of this task, it is my homework I am trying to do. And it specifically asks to continue loop till the correct input is entered. If you didn't notice, this site for the people trying to understand and learn and not show off with jerk comments. If the questions here are ridiculous for you, do everybody a favour and chose other site.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by Nurlana View Post
    And it specifically asks to continue loop till the correct input is entered.
    And how does that justify your misuse of exceptions in any way? That's not a trick question. It doesn't.

    Quote Originally Posted by Nurlana View Post
    this site for the people trying to understand and learn and not show off with jerk comments.
    Welcome to the real world, cupcake, where people who know more than you do about something will not treat you like a precious adorable little flower. If you do something stupid in code, someone with more experience will tell you off for it - I've even seen people lose jobs over such things.

    And if you think I've handled you poorly .... wait until you encounter some other members who have an even lower tolerance for stupidity than I do.
    Last edited by grumpy; 02-23-2013 at 04:30 PM.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Nurlana View Post
    Well, at least you lived to your username. Did it make your day adding nasty note at the end? I didn't thought of this task, it is my homework I am trying to do. And it specifically asks to continue loop till the correct input is entered. If you didn't notice, this site for the people trying to understand and learn and not show off with jerk comments. If the questions here are ridiculous for you, do everybody a favour and chose other site.
    Come on. Take it with a grain of salt. No one threw any bad words at you, so it should all be fine.
    No one here will benefit from making any enemies. I think you will find most people here knowledgeable and willing to help if you just keep a little open mind. Don't let it put you off.
    Instead of, "you are mean," ask "what am I doing wrong, and how can I fix it?"
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Do while loop
    By prasdhy2008 in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 06:02 PM
  2. loop problem
    By eagle0eyes in forum C Programming
    Replies: 7
    Last Post: 05-29-2010, 02:14 PM
  3. Problem in the loop
    By PunchOut in forum C Programming
    Replies: 2
    Last Post: 11-30-2008, 02:17 PM
  4. Plz help with this loop problem,Thanks!
    By blue_blue in forum C Programming
    Replies: 4
    Last Post: 04-28-2008, 11:34 PM
  5. problem with 'for' loop
    By kirklandkonnect in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2006, 10:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21