Thread: do/while loop

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0

    Question do/while loop


    Have a do/while loop which will not terminate, if the second entry is correct.
    PHP Code:
        do
        {
        
    cout <<"\nPlease enter the grade for the 1st exam:\t";
        
    cin >> exam1;

        
    cout <<"Please enter the grade for the 2nd exam:\t";
        
    cin >> exam2;
        
        
    cout <<"Please enter the grade for the 3rd exam:\t";
        
    cin >> exam3;
        
        
    cout <<"Please enter the grade for the 4th exam:\t";
        
    cin >> exam4;

        
    //Check validity of input
        
    error ExamError(exam1exam2exam3exam4);
        
        if (
    error){
        
    cout <<"\nYou have entered an invalid exam score!"
             
    <<"\nWould you like to try again (Y/N): ";
        
    cin >> response;}

        else
            
        {
    FinalGrade[i] = ((exam1*Exam1Percent) + (exam2*Exam2Percent) + (exam3*Exam3Percent) + (exam4*Exam4Percent));}

    Any suggestion? Thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0
    Sorry, here is the full code.
    PHP Code:
        do
        {
        
    cout <<"\nPlease enter the grade for the 1st exam:\t";
        
    cin >> exam1;

        
    cout <<"Please enter the grade for the 2nd exam:\t";
        
    cin >> exam2;
        
        
    cout <<"Please enter the grade for the 3rd exam:\t";
        
    cin >> exam3;
        
        
    cout <<"Please enter the grade for the 4th exam:\t";
        
    cin >> exam4;

        
    //Check validity of input
        
    error ExamError(exam1exam2exam3exam4);
        
        if (
    error){
        
    cout <<"\nYou have entered an invalid exam score!"
             
    <<"\nWould you like to try again (Y/N): ";
        
    cin >> response;}

        else
            
        {
    FinalGrade[i] = ((exam1*Exam1Percent) + (exam2*Exam2Percent) + (exam3*Exam3Percent) + (exam4*Exam4Percent));}


        } while ((
    response == 'Y') || (response == 'y')); 
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    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?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    New York
    Posts
    8
    Rep Power
    0
    The response variable is initialized:
    PHP Code:
    char response
    How do I initialize it to both 'N' and 'n'
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    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';
  12. #7
  13. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by magdaolsen
    The response variable is initialized:
    PHP Code:
    char response
    How do I initialize it to both 'N' and 'n'
    A variables can not be initiliaized to more than 1 value, as it can only store 1 value at any given time.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.

IMN logo majestic logo threadwatch logo seochat tools logo