need help with c++ program please

the program is suppose to calculate the mpg of 10 vehicles, and then average the total mpg for all 10 vehicles. i wrote the program, but it only calculates the vehicles mpg, but i doesn't know how to make it calculate the average off all vehicles.. can someone help plz..

HERE IS THE CODE

int main()
{
int gallons;
double distance;
double gallon;

cout << "Please input how many gallons of gas is in vehicle 1:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 1 traveld.::";
cin >> distance;

cout << "Your vehicle 1 MPG is:" << (distance/gallons) << endl;



cout << "Please input how many gallons of gas is in vehicle 2::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 2 traveld.::";
cin >> distance;

cout << "Your vehicle 2 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 3:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 3 traveld.::";
cin >> distance;

cout << "Your vehicle 3 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 4:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 4 traveld.::";
cin >> distance;

cout << "Your vehicle 4 MPG is:" << (distance/gallons) << endl ;


cout << "Please input how many gallons of gas is in vehicle 5:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 5 traveld.:::";
cin >> distance;

cout << "Your vehicle 5 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 6:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 6 traveld.:::";
cin >> distance;

cout << "Your vehicle 6 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 7:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 7 traveld.:::";
cin >> distance;

cout << "Your vehicle 7 MPG is:" << (distance/gallons) << endl;

cout << "Please input how many gallons of gas is in vehicle 8:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 8 traveld.:::";
cin >> distance;

cout << "Your vehicle 8 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 9:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 9 traveld.:::";
cin >> distance;

cout << "Your vehicle 9 MPG is:" << (distance/gallons) << endl;


cout << "Please input how many gallons of gas is in vehicle 10:::";
cin >> gallons;
cout << "Please input the distance in miles vehicle 10 traveld.:::";
cin >> distance;

cout << "Your vehicle 10 MPG is:" << (distance/gallons) << endl;
return 0;

}
well look what you are doing...
two variables 1 ) gallons , 2 ) distance.
each vehicle you are resetting that value and then you are only outputting the mpg not setting it to a variable.
what you should do is 1 ) create an array to store the gallons , distances, and mpgs. 2 ) create a vector to store the galons , distances, and mpgs.
ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    std::vector<int> gallon( 10 , 0 ) , distance( 10 , 0 );
    std::vector<double> mpg( 10 , 0 );
    // int gallon[ 10 ] = { 0 } , distance[ 10 ] = { 0 }; 
    //double mpg[ 10 ] = { 0.0 };
    //or this ^                              ^
    for( auto i = 0; i < 10; ++i )
    {
         std::cout << "Input gallons for car " << i + 1 << std::cout;
         std::cin >> gallon[ i ];
         //ect
         //ect
         mpg[ i ] = distance[ i ] / gallon[ i ];
    }
}


You can finish the rest of it =p
Last edited on
i don't get it.. sorry i am like a beginner in this stuff. can you please enplane to me what i have to do.
thnx
You have to assign them to a different variable and as I did I used a loop auto is the same thing as get the variable type for me automatically because I was feeling lazy. In reality that is the same as an int. So you don't have to retype everything over and over like you are currently doing.
for example,

vehicle 1, do
int gallons1;
double miles1;
double mpg1;

for vehicle 2, do
int gallons2;
double miles2;
double mpg2;

etc.

at the end, do

double mpgAverage = mpg1 + mpg2 + mpg3...etc... / 10
once again that is doing it the very hard way.....
how about this??? is it correct
int main()
{
int gallons;
double distance, total;
for(int i = 0; i < 10; i++)
{
cout << "Please input how many gallons of gas is in vehicle ::: ";
cin >> gallons;
cout << "Please input the distance in miles vehicle traveled.::: ";
cin >> distance;
cout << "Your vehicle " << i+1 << " MPG is:" << (distance/gallons) << endl;
total += distance/gallons;
}
return 0;
}
you still need to put your values into a container try a vector or array like I mentioned earlier. The for loop is correct though. Also you should indent.
try something like
1
2
double gallon[ 10 ] , distance[ 10 ];
//instead of double gallon , distance 

Then when you input do this
1
2
3
4
cin >> gallon[ i ];
cin >> distance[ i ];
//do same thing for the distance/gallons in the output and total
//this is so you can access the element at that position 
If you set up a for loop:

1
2
3
4
5
6
7
for (int i = 0; i < 10; i++)
{

you can use the for loop here to cleanup and not have the reiteration of all the separate cars.  

cout<<" Your vehicle " <<i <<" MPG is: <<(distance/gallons) <<endl;
} 


then you need to add up the distance/gallons calculations, so you can create a variable for it and then += that variable or do a for loop to tally it up.
sorry if its already been suggested. . . just trying to help out
Topic archived. No new replies allowed.