•
•
•
•
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
![]() |
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);
•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- problem with loop (C++)
- a for loop problem (Java)
- Problem with do-while loop (C++)
- problem with MATLAB loop (Legacy and Other Languages)
- Problem with while loop... probably common (C++)
- how to use loop in this question (C++)
- problem with loop back (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: string array, if statement, and comparisons
- Next Thread: gtkmm and dev-c++ or mingw



Linear Mode