?
Solved

do..while loop

Posted on 2004-08-16
3
Medium Priority
?
303 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

650 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