Solved

do..while loop

Posted on 2004-08-16
3
266 Views
Last Modified: 2010-04-01
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
Comment
Question by:jijou
3 Comments
 
LVL 4

Expert Comment

by:nagraves
ID: 11818403
I have a feeling the error is in that second "some code extracted"
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 50 total points
ID: 11820254
> 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
 

Author Comment

by:jijou
ID: 11826845
Dear rstaveley,

Your answer works great!! THANKS!!

Regards,
jijou
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question