|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
Have a do/while loop which will not terminate, if the second entry is correct.
PHP Code:
Any suggestion? Thanks! |
|
#2
|
||||
|
||||
|
Where's the while condition? I do not see any way to ever get out of the loop.
BTW, can you use do without a while condition? I didn't even know that would compile. |
|
#3
|
|||
|
|||
|
Sorry, here is the full code.
PHP Code:
|
|
#4
|
||||
|
||||
|
What's the value of response before the do loop. Is it explicitly initialized to 'n'/'N' or not, otherwise there could be a problem. Let's say response='Y' before the do loop. If you enter valid entries, then this section of code:
Code:
if (error){
cout <<"\nYou have entered an invalid exam score!"
<<"\nWould you like to try again (Y/N): ";
cin >> response;}
never gets executed and the value of response will stay the way it was before. Hence it'll just loop again. Is this what is causing your problem? |
|
#5
|
|||
|
|||
|
The response variable is initialized:
PHP Code:
How do I initialize it to both 'N' and 'n' |
|
#6
|
||||
|
||||
|
Just declaring a variable does NOT automatically initialize it.
>> How do I initialize it to both 'N' and 'n' You just need to initialize it to one or the other. Try declaring your variable like this: char response = 'n'; |
|
#7
|
||||
|
||||
|
Quote:
__________________
Jason Doucette / Xona.com™ - Programming Windows Errata Addendum "Discussion is an exchange of knowledge; argument is an exchange of ignorance." |
|
#8
|
||||
|
||||
|
I think our question should be: how is the program supposed to work? I.e., what is the normal behavior that we should expect?
You are iterating through a for-loop which executes this do-while loop for each student. I assume that there are only four exam scores for each student. We enter the four exam scores. If an invalid score has been entered for any of the four exams, then the error message is displayed and the data-entry person is allowed to request (by entering a 'Y' or 'y') reentering all four scores for that student. If all four scores entered are valid, then the program calculates the final score and drops out of the do-while loop and iterates to the next student. Please correct me if I misinterpreted your code's intent. The problem that I see is that there is no explicit exit condition if all four exam scores are correct. Because of the test for a yes response, there is an implicit exit condition: response being something other than a 'Y' or a 'y'. The first time through, because response is a local variable that has not been initialized, it has an undefined value (ie, whatever garbage was left there on the stack) which is most probably neither a 'Y' nor a 'y'. So as long as no error is made in data entry, the program should behave as expected, dutifully dropping out of the do-while loop as soon as each student's four scores are entered. However, as soon as an error is detected and the user responds with yes, you'll never get out of that do-while loop again. Because from that point on, response will always contain the 'Y' or 'y' that had been entered. A sneak-path out of the loop exists: just enter an invalid exam score and respond with 'n'. My suggestion: At the top of the do-while loop, re-initialize response with a non-yes value. Or, put an else on that if (error) that will set response to a non-yes value if no error has been detected. BTW, an initialization note: 1. If a variable has file scope (ie, it is declared outside of a function), then it resides in memory and is usually initialized to zero (but don't count on every compiler doing this for you). 2. If a variable has function scope (ie, it is declared inside a function), then it resides on the stack and is not initialized unless you explicitly initialize it (eg, char response = 'n';). These variables also lose their value as soon as you exit the function. If you explicitly initialize one of these variables, then that initial value will be assigned to it every time you enter the function. 3. If a variable has function scope but is declared to be static, then it resides in memory and retains its value between function calls. If you explicitly initialize one of these static variables, then that initial value will be assigned to it the first time you call the function (actually even before that) and then thereafter it will retain the value last assigned to it. Hope all that helps. Last edited by dwise1_aol : April 9th, 2003 at 08:22 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > do/while loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|