C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 9th, 2003, 02:53 PM
magdaolsen magdaolsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 8 magdaolsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old April 9th, 2003, 03:01 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
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.

Reply With Quote
  #3  
Old April 9th, 2003, 03:25 PM
magdaolsen magdaolsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 8 magdaolsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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')); 

Reply With Quote
  #4  
Old April 9th, 2003, 03:35 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
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?

Reply With Quote
  #5  
Old April 9th, 2003, 03:41 PM
magdaolsen magdaolsen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: New York
Posts: 8 magdaolsen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The response variable is initialized:
PHP Code:
 char response

How do I initialize it to both 'N' and 'n'

Reply With Quote
  #6  
Old April 9th, 2003, 03:56 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 h 49 m 40 sec
Reputation Power: 797
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';

Reply With Quote
  #7  
Old April 9th, 2003, 06:31 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 6
Quote:
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.

Reply With Quote
  #8  
Old April 9th, 2003, 08:18 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 55 m 53 sec
Reputation Power: 437
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > do/while loop


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |