• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

do..while loop

Can a do..while loop contain another do..while loop?

I have a function ValidateName() with the following code:
----------------------------------------------------------------------------------------------------------------------
string ValidateName(void)
{
                string temp;
      int i, count;
      bool ValidName = false;

      temp = "";
      getline(cin, temp);

      do
      {
            count = 0;
            for (i = 0; i < temp.length(); i++)
            {
                  // check for non-character/non-space inputs and length of input
                  if ((isalpha(temp[i]) == 0 && isspace(temp[i]) == 0) || (temp.length() < 3))
                  {
                        count = 1;
                        i = temp.length(); // exit the for loop
                  }
            }

            if (count == 0)
                  ValidName = true;
            else
            {
                  ValidName = false;
                  cout << "Invalid input. Try again! ";
                  getline(cin, temp);
            }

      } while (ValidName == false);
      
      return temp;
}
----------------------------------------------------------------------------------------------------------------------

This is my MAIN function:
----------------------------------------------------------------------------------------------------------------------
// some code extracted

int i, j;
string PName = "\0";
char SeatClass, MoreBooking;
bool NameExist = false;

do // FIRST DO..WHILE LOOP
{
      cout << "\nEnter a name: ";
      PName = ValidateName(); // validate name

      do // check if input name already exist in list (SECOND DO..WHILE LOOP)
      {
            NameExist = false;
            for (i = 0; i < 20; i++)
                  for (j = 0; j < 3; j++)
                        // name already exist
                        if (PName == SeatName[i][j])
                        {
                              i = 19; // go to end of loop
                              j = 2;
                              NameExist = true;
                              cout << "\nName exist. Reenter.\n";
                              PName = ValidateName();
                        }
      } while (NameExist == true);

               // some code extracted

      cout << "\n More: Y/N? ";
      MoreBooking = readChar(); // validate user response
      while (MoreBooking != 'Y' && MoreBooking != 'y' && MoreBooking != 'n' && MoreBooking != 'N')
      {
            cout << "Retry .. Only Y or N is accepted " << endl;
            MoreBooking = readChar();
      }
} while (MoreBooking == 'Y' || MoreBooking == 'y');
----------------------------------------------------------------------------------------------------------------------

The first time the main function executes, it works fine: it will go to the second line in the FIRST DO..WHILE LOOP and wait for user input. At the end, if user key in 'Y' in response to "More: Y/N?", it will restart the FIRST DO..WHILE LOOP. But this time, it will skip the 2nd line and go straight into the SECOND DO..WHILE LOOP where it will run the double 'for' loop. And the message "Name exist. Reenter." will come out, before it takes the user input.

Is there something wrong with my code structure or logic? Any help will be very much appreciated.
0
jijou
Asked:
jijou
1 Solution
 
nagravesCommented:
I have a feeling the error is in that second "some code extracted"
0
 
rstaveleyCommented:
> Can a do..while loop contain another do..while loop?

I suspect that your readChar function is doing soething like reading a character and leaving the whitespace in cin for the next getline to get confused by.

If you replace your readChar function with:

--------8<--------
char readChar()
{
     string temp;
     getline(cin, temp);
     return toupper(temp[0]);
}
--------8<--------

...which requires you to hit enter after entering your character (but which flushes all characters including the newline character from cin).

If this makes things behave let us know and we can start nudging you in the right direction.
0
 
jijouAuthor Commented:
Dear rstaveley,

Your answer works great!! THANKS!!

Regards,
jijou
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now