Thread: Do-ing loops , While I shouldn't be

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    Red face Do-ing loops , While I shouldn't be

    I cant not get ths prgram to stop asking for input. I don't know when the problem is . I think it's in the do-while statement, but I can not figure it out. I have not included all the state initials (too long). What you reckon???
    Code:
    #include <iostream>
    #include <string>
    #include <ctype>
    
    void PrintDescription();
    void InputInitials(char&, char&);
    void GetStateName(char&, char&);
    void HoldScreen();
    
    
    int main()
    {
    
      char initial_1;
      char initial_2;
    
      PrintDescription();
      do
      {
    
         GetStateName(initial_1, initial_2);
    
      }while(true);
      cin.get();
      HoldScreen();
      return 0;
    }
    //------------------------------------------------------------------------------
    /* This function outputs a description of the program to the user */
       void PrintDescription(/* out */)
       {
          cout << "\nThis program inputs a two-letter abbreviation for one of "
                  "the 50 states and \n";
          cout << "prints out the full name of the state. If the abbreviation"
                  " is not valid \n";
          cout << "the program will output an error message and ask for the "
                  "abbreviation again.\n";
          return;
       }
    //------------------------------------------------------------------------------
    // This function prompts user for the initials of a State name
      void InputInitials(/* out */char& initial_1,
                         /* out */char& initial_2)
       {
    
          cout << "\nPlease enter the States two initial.\n" << '\n';
          cin >> initial_1 >> initial_2;
          cin.ignore(10, '\n');
    
         initial_1 = toupper(initial_1);
         initial_2 = toupper(initial_2);
    
         return;
       }
    //------------------------------------------------------------------------------
    // This function prints out the states full name
       void GetStateName(/* out */char& initial_1,
                         /* out */char& initial_2)
       {
    
         InputInitials(initial_1, initial_2);
    
         switch(initial_1)
         {
            case 'A':
            {
               switch (initial_2)
               {
                  case 'K' :
                       cout << "Alaska";
                               break;
                  case 'L' :
                       cout << "Alabama";
                               break;
                  case 'R' :
                       cout << "Arkansas";
                               break;
                  case 'Z' :
                       cout << "Arizona";
                               break;
                  default:
                       cout << "\nInvalid abbreviation entered.\n";
               }
               break;
            }
            case 'C':
            {
               switch(initial_2)
               {
                  case 'A' :
                       cout << "California";
                               break;
                  case 'O' :
                       cout << "Colorado";
                               break;
                  case 'T' :
                       cout << "Connecticut";
                               break;
                  default  :
                       cout << "\nInvalid abbreviation entered.\n";
               }
               break;
            }
            case 'D':
            {
               switch (initial_2)
               {
                  case 'E' :
                       cout << "Delaware";
                               break;
                  default  :
                       cout << "\nInvalid abbreviation entered.\n";
              }
              break;
           }
           default  :
                     cout << "\nInvalid abbreviation entered.\n";
           return;
        }
     }
    //------------------------------------------------------------------------------
       void HoldScreen(/* out */)
       {
    
    
         cout << "\nDesigned by Loranne Wish. " << '\n';
         cout << "Press enter to continue.";
         cin.get();
         return;
       }
    It's all about LOVE

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
     do
      {
    
         GetStateName(initial_1, initial_2);
    
      }while(true);
    So: do something, while true. Infinite loop, eh?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Code:
    do
      {
    
         GetStateName(initial_1, initial_2);
    
      }while(true);
    the do-while loop will stop when the condition in the while brackets becomes false (or when a break is encountered)
    since there are no breaks in the loop, the first debug step is to say to your self, "self, when is the condition false"

    well, since 'true' is never 'false' you'll need to change something.

    here's a suggestion, make GetStateName() and InputInitials boolean functions that returns false when there is no input. then the loop can become...
    Code:
    while(GetStateName(initial_1, initial_2));
    
    .....
    
    bool GetStateName(/* out */char& initial_1,
                         /* out */char& initial_2)
       {
    
         if(!InputInitials(initial_1, initial_2))
                  return false;
    .......
    there is probably a more straight forward approach than what i recommended, just think through what your program is doing one step at a time
    hope that helps!

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    Thanks, but the program is suppsoe to prompt the user until a valid abbreviation is entered. Then it;s suppose to end, my (dare I say) program keeps prompting the user and never wants to end.
    It's all about LOVE

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Loraswish
    Thanks, but the program is suppsoe to prompt the user until a valid abbreviation is entered. Then it;s suppose to end, my (dare I say) program keeps prompting the user and never wants to end.
    So use Perspective's example (or variation of). You need to get rid of the while true test and make it while (GetInput() is not good);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM