943,973 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1503
  • C++ RSS
Sep 25th, 2007
0

problem with do...while loop

Expand Post »
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!

C++ Syntax (Toggle Plain Text)
  1.  
  2. do
  3. {
  4. cout << "Please enter a student indentifier from the list (or 'E' to exit): ";
  5. cin >> studentId;
  6.  
  7. length = studentId.length();
  8.  
  9. for (int index = 0; index < NUM_STUDENT_INITIALS; index++)
  10. studentId[index] = toupper(studentId[index]);
  11.  
  12.  
  13. if ((length == 1) && (studentId[0] == 'E') || (studentId[0] == 'e'))
  14. {
  15. cout << endl << "You have choosen to exit the program. Have a nice day."
  16. << endl << endl;
  17. return;
  18. }
  19.  
  20. else if (length > LENGTH_STUDENT_ID)
  21. {
  22. cout << "I'm sorry, " << studentId << " is too long. Please choose a"
  23. << " a correct identifier from the list. All indentifiers are"
  24. << " three letters followed by one number." << endl << endl;
  25. cout << "Enter the student identifier (or 'E' to exit): ";
  26. cin >> studentId;
  27. }
  28.  
  29. else if (length < LENGTH_STUDENT_ID)
  30. {
  31. cout << "I'm sorry, " << studentId << " is too short. Please choose a"
  32. << " a correct identifier from the list. All indentifiers are"
  33. << " three letters followed by one number." << endl << endl;
  34. cout << "Enter the student identifier (or 'E' to exit): ";
  35. cin >> studentId;
  36. }
  37.  
  38. else if (index == 0)
  39. {
  40. while (index < NUM_STUDENT_INITIALS)
  41. {
  42. if((studentId[index] != 'A' ) && (studentId[index] != 'B') &&
  43. (studentId[index] != 'C' ) && (studentId[index] != 'D') &&
  44. (studentId[index] != 'E' ) && (studentId[index] != 'F') &&
  45. (studentId[index] != 'G' ) && (studentId[index] != 'H') &&
  46. (studentId[index] != 'I' ) && (studentId[index] != 'J') &&
  47. (studentId[index] != 'K' ) && (studentId[index] != 'L') &&
  48. (studentId[index] != 'M' ) && (studentId[index] != 'N') &&
  49. (studentId[index] != 'O' ) && (studentId[index] != 'P') &&
  50. (studentId[index] != 'Q' ) && (studentId[index] != 'R') &&
  51. (studentId[index] != 'S' ) && (studentId[index] != 'T') &&
  52. (studentId[index] != 'U' ) && (studentId[index] != 'V') &&
  53. (studentId[index] != 'W' ) && (studentId[index] != 'X') &&
  54. (studentId[index] != 'Y' ) && (studentId[index] != 'Z'))
  55. {
  56. cout << endl
  57. << "I'm sorry, student identifier " << studentId << " is formatted"
  58. << " incorrectly. All" << endl << "indentifiers are three letters"
  59. << " followed by one number. Please" << endl << "try again."
  60. << endl << endl;
  61. cout << "Enter your account number (or 'E' to exit): ";
  62. cin >> studentId;
  63. index = NUM_STUDENT_INITIALS;
  64. }
  65.  
  66. else
  67. {
  68. index++;
  69. }
  70. }
  71. }
  72.  
  73. else if (!(studentId[index] > 0))
  74. {
  75. cout << endl
  76. << "I'm sorry, student identifier " << studentId << " is formatted"
  77. << " incorrectly. All" << endl << "indentifiers are three letters"
  78. << " followed by one number. Please" << endl << "try again."
  79. << endl << endl;
  80. cout << "Enter your account number (or 'E' to exit): ";
  81. cin >> studentId;
  82. }
  83.  
  84. else
  85. looping = false;
  86. } while (looping);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
teppuus is offline Offline
47 posts
since Sep 2007
Sep 25th, 2007
0

Re: problem with do...while loop

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 25th, 2007
0

Re: problem with do...while loop

Ok, thank you....I am trying that now.
Reputation Points: 10
Solved Threads: 0
Light Poster
teppuus is offline Offline
47 posts
since Sep 2007
Sep 25th, 2007
0

Re: problem with do...while loop

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
teppuus is offline Offline
47 posts
since Sep 2007
Sep 25th, 2007
0

Re: problem with do...while loop

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
teppuus is offline Offline
47 posts
since Sep 2007
Sep 25th, 2007
0

Re: problem with do...while loop

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

I mean
C++ Syntax (Toggle Plain Text)
  1. do
  2. {
  3. cout << "Please enter a student indentifier from the list (or 'E' to exit): ";
  4. cin >> studentId;
  5. cout << studentId;
  6. } while (looping);
would have been enough to prove the point.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: string array, if statement, and comparisons
Next Thread in C++ Forum Timeline: gtkmm and dev-c++ or mingw





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC