Solved

do..while loop

Posted on 2004-08-16
3
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

717 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