Solved

do..while loop

Posted on 2004-08-16
3
248 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
Comment Utility
I have a feeling the error is in that second "some code extracted"
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 50 total points
Comment Utility
> 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
Comment Utility
Dear rstaveley,

Your answer works great!! THANKS!!

Regards,
jijou
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now