Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6094497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:41:09+00:00 2026-05-23T12:41:09+00:00

I am trying to make a program which does a very basic calculation, but

  • 0

I am trying to make a program which does a very basic calculation, but for some reason i can’t get the code right. It is supposed to calculate the miles per gallon for one trip. You can then add this info multiple times (for different trips) and for each time it should calculate the total miles per gallon (i.e. the average miles per gallon of all the trips). This is the code:

#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
int counter = 1;
double milePerRe, milePerTo = 0, x, y;
cout << "Enter the miles used (-1 to quit): ";
cin >> x;
cout << "Enter gallons: ";
cin >> y;
while (x != -1)
{
      milePerRe = x/y;
      milePerTo += milePerRe;
      milePerTo /= counter;
      cout << "MPG this tankful: " << setprecision( 6 ) << fixed << milePerRe;
      cout << "\nTotal MPG: " << setprecision( 6 ) << fixed << milePerTo << endl << endl;
      counter++;
      cout << "Enter the miles used (-1 to quit): ";
      cin >> x;
      if (x != -1)
      {
      cout << "Enter gallons: ";
      cin >> y;
      }
}
system("pause");
return 0;
}

When I run the program and say I enter 10 on the miles and 1 on the number of gallons the first time and the second time, everything works fine. Then if i do it again a third time the calculations begin to become incorrect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:41:10+00:00Added an answer on May 23, 2026 at 12:41 pm

    You can not calculate average of averages the way you do it. In your code you are dividing by the counter EACH iteration, while you should divide it only at the end.
    Best way to do what you need is something like this:

    ...
    double totalMiles = 0;
    double totalGallons = 0;
    ...
    while (x != -1)
    {
         milePerRe = x/y;
         totalMiles += x;
         totalGallons += y;
         milesPerTo = totalMiles / totalGallons;
    ...
    

    However, if your task was to explicitly calculate the average of trips (not the average of miles/gallons), you would need to introduce another variable, like this:

    ...
    double currentMilesPerTo;
    ...
    while (x != -1)
    {
        milePerRe = x/y;
        milePerTo += milePerRe;
        currentMilesPerTo = milePerTo/counter;
        ....
        cout << "\nTotal MPG: " << currentMilesPerTo;
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a program which creates a spaceship and I'm using the
I am trying to make a find, find next function for my program, which
I am trying to make my program ignore Ctrl + C in unix which
I'm trying to make a program in Visual C# that has my one created
I am trying to make a program that automatically corrects the perspective of a
I am trying to make a program to work on top of an existing
I'm trying to make a program that reads the value at a certain address.
I am trying to make a program in Java that checks for three specific
Hey, I am trying to make a program that minimises any program to the
I'm trying to make a setup program for an ASP.NET web site. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.