Hello, I am having problem with my do...while loop. It asks for student identifier twice before proceeding. I tried moving the "do" after the program prompts the user for his choice of id, but I couldn't get the loop to terminate after that. Any tips will be appreciated. Thank you!

do
  {
  cout << "Please enter a student indentifier from the list (or 'E' to exit): ";
  cin >> studentId;
  
  length = studentId.length();
  
  for (int index = 0; index < NUM_STUDENT_INITIALS; index++)
      studentId[index] = toupper(studentId[index]);    
  
  
  if ((length == 1) && (studentId[0] == 'E') || (studentId[0] == 'e'))
  {
    cout << endl << "You have choosen to exit the program. Have a nice day."
         << endl << endl;
     return;
  }
  
  else if (length > LENGTH_STUDENT_ID)
  {
       cout << "I'm sorry, " << studentId << " is too long. Please choose a" 
            << " a correct identifier from the list. All indentifiers are"
            << " three letters followed by one number." << endl << endl; 
       cout << "Enter the student identifier (or 'E' to exit): ";
       cin >> studentId;
  }
  
  else if (length < LENGTH_STUDENT_ID)
  {
       cout << "I'm sorry, " << studentId << " is too short. Please choose a" 
            << " a correct identifier from the list. All indentifiers are"
            << " three letters followed by one number." << endl << endl; 
       cout << "Enter the student identifier (or 'E' to exit): ";
       cin >> studentId;
  }

  else if (index == 0)
  {
    while (index < NUM_STUDENT_INITIALS)
    {
       if((studentId[index] != 'A' ) && (studentId[index] != 'B') &&
          (studentId[index] != 'C' ) && (studentId[index] != 'D') &&
          (studentId[index] != 'E' ) && (studentId[index] != 'F') &&
          (studentId[index] != 'G' ) && (studentId[index] != 'H') &&
          (studentId[index] != 'I' ) && (studentId[index] != 'J') &&
          (studentId[index] != 'K' ) && (studentId[index] != 'L') &&
          (studentId[index] != 'M' ) && (studentId[index] != 'N') &&
          (studentId[index] != 'O' ) && (studentId[index] != 'P') &&
          (studentId[index] != 'Q' ) && (studentId[index] != 'R') &&
          (studentId[index] != 'S' ) && (studentId[index] != 'T') &&
          (studentId[index] != 'U' ) && (studentId[index] != 'V') &&
          (studentId[index] != 'W' ) && (studentId[index] != 'X') &&
          (studentId[index] != 'Y' ) && (studentId[index] != 'Z'))
       {
       cout << endl
            << "I'm sorry, student identifier " << studentId << " is formatted" 
            << " incorrectly. All" << endl << "indentifiers are three letters" 
            << " followed by one number. Please" << endl << "try again."
            << endl << endl; 
       cout << "Enter your account number (or 'E' to exit): ";
       cin >> studentId;
       index = NUM_STUDENT_INITIALS;
       }
       
       else 
       {
            index++;
            }
    }
  }

  else if (!(studentId[index] > 0))
  {
    cout << endl
            << "I'm sorry, student identifier " << studentId << " is formatted" 
            << " incorrectly. All" << endl << "indentifiers are three letters" 
            << " followed by one number. Please" << endl << "try again."
            << endl << endl; 
       cout << "Enter your account number (or 'E' to exit): ";
       cin >> studentId;
  }
 
  else
      looping = false;
 } while (looping);

Debugging is an art in itself. To debug this type of problem I'd consider any and/or all of the following until I figured it out:

1) start commenting out one section of the if/else statements at a time to see where the second request statement is coming from, or

2) I'd toss in debugging code that I'd remove once the problem was found. This is usually a cout statement telling me where I am in the code such as "checking for E/e", "too long", "too short", "illegal char input", etc. This way I can see which debugging statement precedes the undesired request for input statement and then I could figure out why it's do this; or

3) I'd comment out everything but the first if statement and the final else adding the internal logic blocks back one at a time to see when the second request for input appears. This mimics writing the code and testing after adding each logic segment to the code, which is probably the best thing to do.

I'm no Picazzo, but this grind it through technique usually get's the job done for me.

Well, your tip is working. I am finding problems I didn't know existed. I will post again once I figure it out in case someone is interested. Thanks again.

Loop is working now. I had to move the do{ after the very first cin >> studentId;. The program needed to calculate the length of the studentId each time a new studentId was entered in. I still have to comment out the last else if statement, but I can figure that one out. Thanks again!

Another approach to debugging is to not write so much code in the first place before trying to run it.

I mean

do
  {
    cout << "Please enter a student indentifier from the list (or 'E' to exit): ";
    cin >> studentId;
    cout << studentId;
  } while (looping);

would have been enough to prove the point.

This article has been dead for over six months. Start a new discussion instead.