C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
I could use a little help, please! Posted by sph_grad2003 on 21 Sept 2006 at 10:03 PM
Hey everyone I'm new to C++ programming and I have a dilemma. How do I get this program to add the totals of all the data entries to get the grand total? I am stumped! Any help would be greatly appreciated!

Thanks,
JR


// -- basic opening commands --


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


const double FLEET_AVG_MPG = 25.0;


// -- variable declarations --


int main()
{
double gallonsUsed,
milesTraveled,
pricePerGallon,
tripMileage,
tripCost,
tripCostPerMile,
totalMiles,
totalGallons,
totalCost,
totalMPG,
lessThan,
greaterThan;
char choice;



cout << "\n\n Fuel Usage Analysis\n";


// -- output statements asking for user input information --


do
{

cout << "\nEnter the number of gallons of fuel: ";
cin >> gallonsUsed;

while (gallonsUsed < 0.0 or gallonsUsed > 40.00)
{
cout << "\nOut of range: must be between 0 and 40 gallons. Re-enter: ";
cin >> gallonsUsed;
}

cout << "\nEnter the number of miles: ";
cin >> milesTraveled;

while (milesTraveled < 0.0 or milesTraveled > 800.0)
{
cout << "\nOut of range: must be between 0 and 800 miles. Re-enter: ";
cin >> milesTraveled;
}

cout << "\nEnter the price per gallon: ";
cin >> pricePerGallon;

while (pricePerGallon < 1.00 or pricePerGallon > 5.00)
{
cout << "\nOut of rang: must be between 1 and 5 dollars. Re-enter: ";
cin >> pricePerGallon;
}


// -- formulas --


tripMileage = (milesTraveled / gallonsUsed);
tripCost = (gallonsUsed * pricePerGallon);
tripCostPerMile = (tripCost / milesTraveled);
totalMiles = (milesTraveled);
totalGallons = (gallonsUsed);
totalCost = (tripCost);
totalMPG = (totalMiles / totalGallons);
lessThan = (FLEET_AVG_MPG - totalMPG);
greaterThan = (totalMPG - FLEET_AVG_MPG);


// -- data set analysis --


cout << fixed << setprecision(2)
<< "\nTrip Mileage: " << tripMileage << " mpg";

cout << fixed << setprecision(2)
<< "\nTrip Cost: $ " << tripCost;

cout << fixed << setprecision(3)
<< "\nTrip Cost per mile: $ " << tripCostPerMile;

cout << "\nAnother? (y or n) ";
cin >> choice;
cout << "\n";
}

while (choice == 'y');


// -- totals of all data sets --


cout << fixed << setprecision(2)
<< "\nTotal Miles: " << totalMiles;

cout << fixed << setprecision(2)
<< "\nTotal Gallons: " << totalGallons;

cout << fixed << setprecision(2)
<< "\nTotal Cost: $ " << totalCost;

cout << fixed << setprecision(2)
<< "\nOverall MPG: " << totalMPG << "\n";



if (totalMPG < 25.0)
cout << "\nYour vehicle's mileage is less than the fleet average by " << lessThan << " mpg";
else if (totalMPG > 25.0)
cout << "\nYour vehicle's mileage is greater than the fleet average by " <<greaterThan << " mpg";
else
cout << "\nYour vehicle's mileage is equal to the fleet average of " << FLEET_AVG_MPG << " mpg";
cout << "\n";




return 0;
}
Report
Re: I could use a little help, please! Posted by bilderbikkel on 22 Sept 2006 at 2:01 AM
* Use code tags
* Shorten up your code
See ya,

bilderbikkel

Report
Re: I could use a little help, please! Posted by eugenek on 22 Sept 2006 at 6:40 AM
: Hey everyone I'm new to C++ programming and I have a dilemma. How do I get this program to add the totals of all the data entries to get the grand total? I am stumped! Any help would be greatly appreciated!
:
: Thanks,
: JR
:

Your formulas section appears to be where the problem is. For example; "totalGallons = (gallonsUsed)" simply makes totalGallons equal to the current value of gallonsUsed. It isn't really keeping track of any totals here. Maybe something like "totalGallons += gallonsUsed" would be more useful?

You could also get the grand total of all entries by using a vector or an array. This would allow you to not only display the totals but the individual entries which make up the totals.

Also, I think it is strange that you created a constant FLEET_AVG_MPG but did not really use it. In the section which indicates whether a mileage is below or under fleet average the value of 25.0 is used instead of the constant. If the average changed how many places would you have to change your code?

I would think you'd want something similar to the following;

if ( totalMPG < FLEET_AVE_MPG )
{
    // Say something;
}
else if ( totalMPG > FLEET_AVE_MPG )
{
    // Say something different;
}
else    // Is equal to FLEET_AVE_MPG
{
    // Say a different something;
}


Report
Re: I could use a little help, please! Posted by sph_grad2003 on 26 Sept 2006 at 6:55 PM
Eugene:

Thank you very much for the tips. They helped out immensely! (I have to take a C++ basics course for my major, meteorology, and I am not a fast learner when it comes to computers and writing programs!) For some reason, this C++ course is required for a lot of majors in our College of Liberal Arts and Sciences department!?

Anyway, thanks again! I received 91/100 points on the assignment!



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.