New C++, need help with program

Hello everybody, I'm new to C++. I'm learning by myself. I've written the program below, and everything works, but now I want to add more to the program. I need help with that. My program below asks the user for gallons and miles driven till the user inputs -1, the the program shows the average gallons per hour for all the trips. Now, the program shows the total of all the trips, and divides by number of trips and shows the result. How do I make it show the total for each? Basically, I need help with the array and for loop, everything else works like I want. Right now, it only shows the last number. Thank you.

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

int main()

{
    float gallons, miles, gph, totalgph, average, counter;
    
    float array[100];

    average=1;
    counter=0;
    gallons=1;
    
    while(gallons!=-1)
    {
     cout<<"Enter the gallons used (-1 to end): ";
     cin>>gallons;
     
     if(gallons==-1)
     break;
     
     else
     {
     cout<<"Enter the miles driven: ";
     cin>>miles;
     
     gph=miles/gallons;
     
     totalgph=totalgph+gph;
     counter++;
     average=totalgph/counter;
     
     for(int x=0; x<counter; x++)
     {
     array[x]=gph;
     }
     
     cout<<"The miles / gallon for this tank was "<<gph<<"\n\n";
     }
    }
    for(int i=0; i<counter; i++)
    {
            cout<<array[i]<<"\n";
    }
    
    cout<<totalgph<<" / "<<counter<<" = "<<average<<"\n";   
    
    getch();
    return 0;
    
}
1
2
3
4
for(int x=0; x<counter; x++)
{
   array[x]=gph;
}
What this does is iterate through all trips you've entered so far and set their totals to your latest entry.
You only need array[counter-1] = gph; to set a single element.
firstly looks like your using an older compiler with the .h in the preprocessor directories, but.. the show must go on.

lets break this down piece by piece.

in programming if you have values such as miles or gallons that would normally be a whole number use int gallons, miles; etc.. saves compiling space in super long code.



1
2
3
4
5
6
7
8
9
10
11
12
while(gallons!=-1)
    {
     cout<<"Enter the gallons used (-1 to end): ";
     cin>>gallons;
     
     if(gallons==-1)
     break;
     
     else
     {
     cout<<"Enter the miles driven: ";
     cin>>miles;


What exactly are you wanting to do with this?

You may want to not allow the user to enter a negative number since that wouldnt make any sense.

so maybe use something like this:

1
2
3
4
5
6
7
8
9
10
if (gallons > 0)
cout << "enter miles driven....;
else
cout << "error, gallons cannot be negative."...

//if you want a sentinal value to stop the code you can make something like.

if (gallons == -1)
return 0;
 


at the end you have getch() which looks like youre getting only a single char.
so you may want to use getline or get char array?

let me know what you want to change to build your proggy.
Last edited on
@nickoolsayz,
He himself said that -1 is to quit the program. cout<<"Enter the gallons used (-1 to end): ";
The getch() is there so that the console doesn't close immediately.
And what kind of space do you think floats take up?

@OP,
It's really just <iostream> (no .h). Also, conio.h is not part of the standard, so don't get too attached to it. See http://cplusplus.com/forum/beginner/1988/ to replace it.
Im very new to c+ as well, so I was only repeating what was taught to me in school. Also I was considering a specific sentinel so the user wouldnt accidentally use a negative number to fault the program. never used getch() befor so wasnt sure of its use.

I made a similar program in class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    /*
    White a program that calculates a cars gas milage. the program should 
    ask the user to enter the number of gallons of gas the car can hold, 
    and the number of miles the car can be driven on a full tank of gas.
    It should then display the number of miles that may driven per gallon of gas.
    */
    int gallons, miles, MPG;
    
    cout <<  "This program is calculating gas mileage for your car. \n";
    cout << "\n";
    
    cout << "How many gallons of gas can your car hold? \n";
    cin >> gallons;
    cout << "How many miles can your car drive on a full tank of gas?\n";
    cin >> miles;
    
    MPG = miles / gallons;
    
    cout << "Your car gets " << MPG << " MPG.\n";
    cout << "Thank you for playing :)" << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
I think in all modern compiler floats are deprecated in favor of doubles (offers more precision but uses more space, not a problem these days) so you can safely use double instead.

Also I would suggest to use a more up-to-date compiler since conio.h and iostream.h are not standard c++. It would help you create code more portable and efficient of new features.

Besides that, don't use getch() instead use cin.get() for the same purpose.

As regards your program itself be cautious with expression that detects real number equality/inequality like
gallons!=-1

Not a good idea at all. Unlike humans computers have some limited perception of numbers and they might create round off errors when calculating real numbers. In other words they cannot be trustworthy in those cases. For compiler 1 might be also equal to 1.000000000000000000001 since they only detect numbers up to a certain precision and latter number excess that precision.

Instead you can safely use conditions like
e < 0 //compares with 0 but not for equality

abs(gallons + 1) < e //where e is a small amount like 1e-5 = 10^-5. This one checks the absolute value (abs) of //gallons + 1 against 0 so basically checks gallons against -1

This latter although looks like relational condition but it virtually checks real numbers equality since it's used as a compensation for reduced compiler's precision.

You don't have to do implement this here since you just want to have an invalid number for loop exit. Use your negative numbers for this.
1
2
if(gallons==-1)
     break;


CAUTION! For int you can safely use them in equality condition like:
if(a == -1)

As for the total for each if I am getting it right you can just use what @hamsterman said and keep them in an array. Just make sure you don't exceed your array's boundary (100 in your case). Maybe also you need to add a condition to check this also.
Something like

1
2
3
4
5
6
int N = 100, n = 0;
while(gallons!=-1 && n < N)
{
 ...
n++;
}
Topic archived. No new replies allowed.