Help!

E

Eric Whittaker

Hey guys, i am relatively new at this programming thing and am stuck on an
assignment here. I'm trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.

//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;

while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}

if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;


;return 0;

}
 
L

Larry I Smith

Eric said:
Hey guys, i am relatively new at this programming thing and am stuck on an
assignment here. I'm trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.

//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;

total = 0.0;

// while ( gal != -1 ) {
for (;;) { // loop forever
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;

if (gal < 0) // quit loop on negative gallons
break;

// ignore 0 gallons to prevent divide-by-zero error in 'mpg' calc
if (gal == 0)
continue;
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}

// if ( gal != 0 )
if (tg > 0)
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;

// ;return 0;
return 0;

Regards,
Larry
 
E

Eric Whittaker

Hey thanks larry, the use of (for) is very helpful there. I'm just trying to
learn here, cause my assignment wants me to use a (while) structure to
accomplish this task, is this possible, when i try it keeps giving me a
"undeclared identifier - gal" error.

Thanks for all your help.
 
J

John Carson

For future reference, a more informative subject line than "Help" would be a
good idea.


Eric Whittaker said:
Hey guys, i am relatively new at this programming thing and am stuck
on an assignment here. I'm trying to get the program below to respond
to the integer -1 to stop and calculate total. It does, but it makes
me enter -1 under both gallons and miles, how can i avoid this?
Thanks.
//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;

Note that the first time gal != -1 is encountered, gal is uninitialised. You
should set it equal to some initial value that is unequal to -1.
while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;

The problem here is that the gal!=-1 test is made *before* the user enters
the gal value, so you have to proceed to the end of the loop before the loop
finishes. You don't actually have to enter -1 under both gallons and miles.
After you enter -1 for gal, you can enter anything for miles and the loop
will still end. You could handle this in various ways. One way is to use

while(true)
{
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
if(gal == -1)
break;

// remainder of loop

}

Another way is to have the initial gallons query before the loop and all
subsequent gallons queries as the final part of the loop.

cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;

while(gal != -1)
{
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;

cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
}

Finally, you could always have one run of the while loop and then ask the
user at the *end* of the loop if they want to continue or quit and use the
answer to set the variable on which your while() condition depends (e.g.,
you could define a bool keepGoing which you initialise to true and set to
false when the user indicates they want to quit, and use while(keepGoing)).
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 )
<< fixed << mpg;
}

if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;

Since you are dividing by tg, I think you should be testing tg!=0, not
gal!=0. You also probably want all three lines in a block, i.e.,

if ( tg != 0 )
{
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
}

;return 0;

What is the ; at the start of the line for?
 
E

Eric Whittaker

you guys are the best, saved me from a couple hour nightmare! i apologize
for the lack of etiquette, as i mentioned i'm new to this. look forward to
talking with ya in the future.
 
L

Larry I Smith

Eric said:
Hey thanks larry, the use of (for) is very helpful there. I'm just trying to
learn here, cause my assignment wants me to use a (while) structure to
accomplish this task, is this possible, when i try it keeps giving me a
"undeclared identifier - gal" error.

Thanks for all your help.

'while(1)' or 'while(true)'
 
P

Peter Koch Larsen

Eric Whittaker said:
Hey thanks larry, the use of (for) is very helpful there. I'm just trying
to learn here, cause my assignment wants me to use a (while) structure to
accomplish this task, is this possible, when i try it keeps giving me a
"undeclared identifier - gal" error.

Thanks for all your help.
Probably a
do
{
....
} while (gallons > 0)

could be appropriate.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top