1.11M Members

Help with while, do while, and for loops

 
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!!

 
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;
   }
 
0
 

tuskyballer ,
Never ever ever use

goto

stmts.

 
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.

 
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.

 
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.

 
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!

 
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.

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article