How to do this?

This is a discussion on How to do this? within the C++ Programming forums, part of the General Programming Boards category; This is my code. I have everything done but this requirement. At one point the user inputs to have their ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    Question How to do this?

    This is my code. I have everything done but this requirement. At one point the user inputs to have their results sorted by either "gas" consumption or "total" cost. SO how would I print: If the users criteria is "Gas", then print the output for the car with the lower gas consumption first. If the criteria is "Total", then print the output for the car with the lowest total cost first.
    Here is my code:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
     
    int main() {
     
     
    int miles_per_year;
    double gallon_gas;
    double hybrid_cost;
    int hybrid_mpg;
    double hybrid_resale;
    double cost_nonhybrid;
    int non_hybrid_mpg;
    double nonhybrid_resale;
    double fuel_five_years; 
    double total_gallons;
    double total_cost;
    double depreciation;
    double fuel_five_years_hybrid; 
    double total_gallons_hybrid;
    double total_cost_hybrid;
    double depreciation_hybrid;
    
    
     
        cout <<"Please answer the following questions.\n" << endl;
         
    do
    {
        cout <<"The estimated miles driven per year?" << endl;
        cin >> miles_per_year;
    if(miles_per_year < 0)
        cout <<"Please enter a positive value." << endl;
    } while(miles_per_year < 0);
     
     
    do
    {
        cout <<"The estimated price of a gallon of gas?" << endl;
        cin >> gallon_gas;
    if(gallon_gas < 0)
        cout <<"Please enter a positive value." << endl;
    } while(gallon_gas < 0);
     
     
    do
    {
        cout <<"The cost of a hybrid car?" << endl;
        cin >> hybrid_cost;
    if(hybrid_cost < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_cost < 0);
     
     
    do
    {
        cout <<"The efficiency of the hybrid car in miles per gallon?" << endl;
        cin >> hybrid_mpg;
    if(hybrid_mpg < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_mpg < 0);
     
     
    do
    {
        cout <<"The estimated resale value for a hybrid after 5 years?" << endl;
        cin >> hybrid_resale;
    if(hybrid_resale < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_resale < 0);
     
     
    do
    {
        cout <<"The cost of a non-hybrid car?" << endl;
        cin >> cost_nonhybrid;
    if(cost_nonhybrid < 0)
        cout <<"Please enter a positive value." << endl;
    } while(cost_nonhybrid < 0);
     
     
    do
    {
        cout <<"The efficiency of the non-hybrid car in miles per gallon?" << endl;
        cin >> non_hybrid_mpg;
    if(non_hybrid_mpg < 0)
        cout <<"Please enter a positive value." << endl;
    } while(non_hybrid_mpg < 0);
     
     
    do
    {
        cout <<"The estimated resale value for a non-hybrid after 5 years?" << endl;
        cin >> nonhybrid_resale;
    if(nonhybrid_resale < 0)
        cout <<"Please enter a positive value." << endl;
    } while(nonhybrid_resale < 0);
         
     
     string buying_crit;
        cout <<"Would you like results sorted by lowest gas consumption(\"gas\")\n or lowest total cost(\"total\")?" << endl;
        cout << "Enter 'gas' or 'total'\n"; 
        cin >> buying_crit;
    
    
    
    
    
    
    
    
    //calculations for hybrid
    total_gallons_hybrid = miles_per_year / hybrid_mpg;
    fuel_five_years_hybrid = miles_per_year / hybrid_mpg * gallon_gas;
    depreciation_hybrid = hybrid_cost - hybrid_resale;
    total_cost_hybrid = fuel_five_years_hybrid + depreciation_hybrid;
    
    
    //calculations for NON-hybrid
    total_gallons = miles_per_year / non_hybrid_mpg;
    fuel_five_years = miles_per_year / non_hybrid_mpg * gallon_gas;
    depreciation = cost_nonhybrid - nonhybrid_resale;
    total_cost = fuel_five_years + depreciation;
    
    
    //for fuel consumption
    if (total_gallons_hybrid < total_gallons)
    {
    cout <<"This is for a Hybrid Car."<< endl;
    cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons_hybrid << endl;
    cout <<"Total cost of owning the car for 5 years: $ " << total_cost_hybrid << endl;
    }
    else 
    	cout <<"This is for a Non-Hybrid Car. "<< //some variable or something here <<endl;
    cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons << endl;
    cout <<"Total cost of owning the car for 5 years: $ " << total_cost << endl;
    
    
    //for cost
    if (total_cost_hybrid < total_cost)
    {
    cout <<"This is for a Hybrid Car."<< endl;
    cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons_hybrid << endl;
    cout <<"Total cost of owning the car for 5 years: $ " << total_cost_hybrid << endl;
    }
    else 
    	cout <<"This is for a Non-Hybrid Car. "<< //some variable or something here <<endl;
    cout <<"Total gallons of fuel used for 5 years: $ "<< total_gallons << endl;
    cout <<"Total cost of owning the car for 5 years: $ " << total_cost << endl;
    
    
    
    
    
    
    
    
    
    
    
    
    system("pause");
    return 0;
     
     
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Quote Originally Posted by anchorblue804 View Post
    This is my code. I have everything done but this requirement. At one point the user inputs to have their results sorted by either "gas" consumption or "total" cost. SO how would I print: [B][COLOR=#444444][FONT=Arial]If the users criteria is "Gas", then print the output for the car with the lower gas consumption first. If the criteria is "Total", then print the output for the car with the lowest total cost first.
    If the user enters GAS, sort in ascending order by gas consumption, then print.
    If the user enters TOTAL, sort in ascending order by total cost, then print.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Yeah I know, but how do I do that in code? I'n new ha

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    There are two steps. First determine whether the user wants "Gas" first or "Total" first.
    It should be a simple comparison like if ( userInput == "Total" ) ... else ...
    The dots are where your display information should go, in the correct order, which you have calculated. I don't think a sort is necessary because I think there is only one kind of each car.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21