# Need Help on Program Using For Loop

• 02-25-2002
Unregistered
Need Help on Program Using For Loop

I/P : 3 integer test grades
O/P: Exam average (float or double)
• 02-25-2002
Post what you have done so far.
• 02-25-2002
Unregistered

#include <iostream.h>
#include <iomanip.h>

int main ()

{

int counter;
double average;

total=0;
counter=0;

for (counter=1;
counter<=3;
counter++)

{
counter= counter+1
}

average= total/counter;
cout<<\n "Your exam average is:" <<average;

cout<<"\n End of lab";

return 0;
}
//end of main function
• 02-25-2002
Govtcheez
Close.

> counter=0;

This is unecessary, since you're just giving counter another value on the next line anyways.

> counter= counter+1

This is also unecessary, since the "counter++" in your for loop will do this. The way you have it now, counter's value is increased by 2 on every pass.

> -1 to end

There's really no reason to have this in, since you don't deal with what happens if they put in -1.

Looks fine, otherwise...
• 02-25-2002
Unregistered
for (counter=1;
counter<=3;
counter++)
==============================
with above instructions the loop will stop when counter == 4. Work it out by hand to prove it to yourself. If you start counter at 0 and increment until counter < 3, then the value of counter will (often) equal the number of grades entered when loop is terminated. This is one reason why loop counters frequently start at 0 rather than one.
===============================

{
================================
ask yourself what value grade has the first time through the loop. It is undefined right? The user hasn't had the chance to enter the grade at this point yet, right? Therefore you will be assigning garbage to total the first time through, ang building on garbage each additional loop through. Also, ask yourself what happens to user input the third time through the loop. Will user input be added to total? Nope. Write out a description for yourself as to why that is so. This line is correct in syntax but inappropriate in location. I'll leave it to you to figure out where it should go.
=================================

counter= counter+1;
=================================
this line should be eliminated as per previous post
==================================

==================================
I actually like the idea here. You are allowing user to terminate the loop early if they have less tha three grades to enter. However, you have to deal with the input of -1 somehow. Here's one way:

{
break;
}

I would actually prefer a slightly different (although not necessarily better) approach to this aspect of the program, but the above sequence should allow flexibility for the user.
==================================

}
• 02-25-2002
Dummies102
would it be easyer to just have:

double a, b, c;
for(;;)//just add this if you want this to loop forever, I know unorthodox, but works.
{
cin>>a>>b>>c;
a = (a+b+c)/3
cout <<"you're GPA is "<<a<<endl;
}

or you could even add something like
if (a > 3.3)
cout <<"you're average is an a";
else if (a < 3.3 && > 2.7)
cout <<"you have a b";
you get the jist.

or do you really have your heart set on a for loop? anyways, hope this helps.
• 02-25-2002
Govtcheez
> but(I forget the boolean function for but)

There is no "boolean function" for but. You want and, which is &&.
• 02-25-2002
Unregistered
Thanks so much for helping!
• 02-25-2002
Unregistered
I have to use a for loop. I know it would be much easier the other way.
---------------------------------------------------------------------------------
Once I think I know what I'm doing, I'm confused again. I really don't know what I'm doing or why. I'm just trying to write the program.
---------------------------------------------------------------------------------
for (counter=1;
counter<=3;
counter++)

So I need to change counter=1 and counter<3 ? I'll try it, but my teacher had that up for an example in class.
---------------------------------------------------------------------------------
{

I'm sorry I don't know what I'm doing here. And I still don't quite understand.

Any more help on this will greatly be appreciated. Thanks!
• 02-26-2002
Unregistered
const int MAX_INPUT = 3;