1,105,295 Community Members

Help with while, do while, and for loops

Member Avatar
tuskyballer
Newbie Poster
2 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm about 3 weeks into my C++ class. I already made a simple program below that calculates grade averages and received an A. Now my teacher wants me to turn in three variations of it, replacing the goto with a while, do while, or for loop. I have experimented and researched for hours and hours, and I am totally frustrated. Here is my program:

#include <iostream>
using namespace std;
void main()
{
int score, total, count;
float average;
total=0;
count=0;
get_next_score:
cout<<"Enter an exam score:  ";
cin>>score;
if(score<0) goto calc_average;
total=total+score;
count=count+1;
goto get_next_score;
calc_average:
average=total/count;
while(score>0)cout<<"The average is "<<average;


}

Any help is greatly appreciated!!

Member Avatar
Dave Sinkula
long time no c
4,852 posts since Apr 2004
Reputation Points: 2,398 [?]
Q&As Helped to Solve: 340 [?]
Skill Endorsements: 69 [?]
Team Colleague
 
0
 

Equivalent loops:

[B]int[/B] i;

   /*
    * #1: while
    */
   i = 0;
   [B]while[/B] ( i < 10 )
   {
      // ...
      ++i;
   }

   /*
    * #2: do...while
    */
   i = 0;
   [B]do[/B]
   {
      // ...
      ++i;
   } [B]while[/B] ( i < 10 );

   /*
    * #3: for
    */
   [B]for[/B] ( i = 0; i < 10; ++i )
   {
      // ...
   }

   /*
    * #4: goto
    */
   i = 0;
label:
   // ...
   ++i;
   [B]if[/B] ( i < 10 )
   {
      [B]goto[/B] label;
   }
Member Avatar
Intel
Light Poster
30 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 9 [?]
 
0
 

tuskyballer ,
Never ever ever use

goto

stmts.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

>void main()
This is not, and never has been, correct C++. The C++ specification requires main to return an int.

>Never ever ever use goto stmts.
Normally I'd call you a moronic conformist, but you've shown yourself to be fairly knowledgeable. So I'll give you a chance to argue your case and then consider taking back the "never ever ever" part before I become the evil voice of reason.

Member Avatar
Intel
Light Poster
30 posts since Feb 2005
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 9 [?]
 
0
 

>void main()
This is not, and never has been, correct C++. The C++ specification requires main to return an int.

>Never ever ever use goto stmts.
Normally I'd call you a moronic conformist, but you've shown yourself to be fairly knowledgeable. So I'll give you a chance to argue your case and then consider taking back the "never ever ever" part before I become the evil voice of reason.

Oh man, you even don't know the down side of "goto" stmt in C++? Crab, well then i should not waste my time replying to your posts.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 80 [?]
Team Colleague
 
0
 

>you even don't know the down side of "goto" stmt in C++?
I'm familiar with both sides of the argument, and I'm in a unique position to argue that goto does have it's uses even though most of the time it should be avoided. An unthinking ban is stupid.

>well then i should not waste my time replying to your posts.
Now you're just bitter that I've been questioning your knowledge.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Oh man, you even don't know the down side of "goto" stmt in C++? Crab, well then i should not waste my time replying to your posts.

The much maligned goto statement has a good and healthy reason to be part of the language. One of the best reasons is the escape from a nested loop. It also makes some code much easier to discern. I personally value both Intel's and Narue's input, so lets get back into the sandbox!

Member Avatar
alc6379
Cookie... That's it
2,752 posts since Dec 2003
Reputation Points: 105 [?]
Q&As Helped to Solve: 154 [?]
Skill Endorsements: 4 [?]
Team Colleague
 
0
 

The much maligned goto statement has a good and healthy reason to be part of the language. One of the best reasons is the escape from a nested loop. It also makes some code much easier to discern. I personally value both Intel's and Narue's input, so lets get back into the sandbox!

Seriously. It's a help forum here. Isn't a little counterproductive to go,

you don't know why you shouldn't use goto? Well, I'm not going to tell you!!! :p

Share the knowledge, and I'll learn a little bit, too.

Member Avatar
Stack Overflow
Junior Poster
193 posts since Sep 2004
Reputation Points: 8 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello,

C provides the infinitely-abusable goto statement, and labels branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it. The goto statement was once widely used. Unfortunately code that used goto extensively was poorly structured and could easily become unmanageable, and is known as "spaghetti code". Today, programming languages provide other ways to structure code so goto is rarely needed. In C++ it is always possible to use something other than goto to achieve what you need.

Note that the use of goto is a religious issue and has provoked a great deal of debate. In C++ there is always a better alternative for writing loops, so you don't need to use goto at all. There are other circumstances where goto may be more appropriate than the alternatives, though that is a seperate subject. Keep in mind when you use it to exit a block which contains aggregates requiring destructors, the destructors will run before the goto transfers control. The compiler still forbids using goto to enter a scope that requires constructors.

The indiscriminate use of goto statements has caused tangled, miserable, impossible-to-read programs. To avoid the use of goto, more sophisticated, tightly controlled looping commands have been introduced: for, while, and do-while. Using these makes programs more easily understood, and goto is generally avoided, but one might argue that the case has been a bit overstated. Like any tool, carefully used and in the right hands, goto can be a useful construct, and the ANSI committee decided to keep it in the language because it has its legitimate uses.


- Stack Overflow

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article