User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,507 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,666 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 824 | Replies: 5
Reply
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

problem with do...while loop

  #1  
Sep 25th, 2007
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);

AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: problem with do...while loop

  #2  
Sep 25th, 2007
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.
Reply With Quote  
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

Re: problem with do...while loop

  #3  
Sep 25th, 2007
Ok, thank you....I am trying that now.
Reply With Quote  
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

Re: problem with do...while loop

  #4  
Sep 25th, 2007
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.
Reply With Quote  
Join Date: Sep 2007
Posts: 47
Reputation: teppuus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
teppuus's Avatar
teppuus teppuus is offline Offline
Light Poster

Re: problem with do...while loop

  #5  
Sep 25th, 2007
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!
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: problem with do...while loop

  #6  
Sep 25th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:39 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC