Thread: Really bad at C++, NEED some help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Really bad at C++, NEED some help

    I'm having trouble with the second part of my programming projects. For the first one I believe I did everything correctly except I don't know what a globally defined constant is. Can somebody show me how to do this and please check if I did it correctly based on the problem. Any help/advice is greatly appreciated

    A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user's car and the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.
    After doing this...
    Modify your program so that it will take input data for two cars and output the number of miles per gallon delivered by each car. Your program will also announce which car has the best fuel efficiency (highest number of miles per gallon).

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    const double GALLONS_PER_LITER = 0.264179;
    
    
    int liters = 0;
    double distance = 0.0;
    
    double mpg = 0.0;
    do
    {
    cout << "Please input how many liters of gasoline is in your vehicle: ";
    cin >> liters;
    
    cout << "Please input the distance in miles you traveled in your vehicle: ";
    cin >> distance;
    
    
    mpg = distance / (liters * GALLONS_PER_LITER);
    
    
    
    cout << "Your vehicle's MPG is: " << mpg << endl;
    
    }while(liters >-1);
    
    return 0;
    }
    I'm totally lost on the second part of this program as well.

    Write a program to gauge the rate of inflation for the past year. The program ask for the price of an item(Such as a hot dog or a one carat diamond) both one year ago and today. It estimates the inflation rate as the difference in price divided by the year ago price. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the rate of inflation. The inflation should be a value of type double giving the rate as a percent, for example 5.3 for 5.3%.
    Enhance your program from the previous programming project by having it also print out the estimated price of the item in one and in two years from the time of the calculation. The increase in cost over one year is estimated as the inflation rate times the price at the start of the year. Define a second function to determine the estimated cost of an item in one year, given the current price of the item and the inflation rate as arguments.

    Code:
    #include <iostream>
    using namespace std;
    
    
    
    int main()
    {
    
         double rate;
         double price_last_year, price_this_year;
    
    
         while(price_last_year != 0.0){
         cout << "Enter price of item from last year: " << endl;
         cout << "Enter 0.0 to quit" << endl; 
         cin >> price_last_year;
            if(price_last_year == 0.0)
               exit(1);
         cout << "Enter price of item for this year: ";
         cin >> price_this_year;
    
         rate = ((price_this_year) - (price_last_year))/price_last_year;
    
         cout << "Rate of inflation is\n" << rate << "%" << endl;
       
         }
    
         return 0;
    }

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    It looks like you're having problems with indentation. Let me show you how I would indent your first program (I haven't really changed anything here):

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
        const double GALLONS_PER_LITER = 0.264179;
    
        int liters = 0;
        double distance = 0.0;
    
        double mpg = 0.0;
    
        do
        {
            cout << "Please input how many liters of gasoline is in your vehicle: ";
            cin >> liters;
    
            cout << "Please input the distance in miles you traveled in your vehicle: ";
            cin >> distance;
    
            mpg = distance / (liters * GALLONS_PER_LITER);
    
            cout << "Your vehicle's MPG is: " << mpg << endl;
        }
        while(liters > -1);
    
        return 0;
    }
    Now I see you have indeed misunderstood how to define a global constant. Let me show you the difference:

    Code:
    #include <iostream>
    
    const double PI = 3.14159265358979; // Global constant
    
    int main()
    {
        const double E = 2.7182818; // Constant belonging to main()
        
        std::cout << "pi * e = " << PI * E << std::endl;
        
        return 0;
    }
    Hopefully this will make it clear what you need to do to fix your code.

    There does seem to be another problem though. When the sentinel value -1 is entered, your program still does the calculation with this value and prints it, but it should probably stop immediately instead. You should probably tell the user about the sentinel value at the beginning as well, or better yet, if you're assignment allows it, not use a sentinel value at all (sentinel values are evil) and ask the user each time whether they want to do another calculation.

    For the second program I don't know how you can say you're totally lost - you've almost finished!

    The first thing to fix is that you're code is not compiling (at least not on my platform). You should be doing #include <cstdlib> if you want to use exit(), but you don't need to use exit() in this case. You're inside main() so you can just do "return 0;" instead of exit() and your program will terminate. Also, if you were to use exit(), in this case you would do exit(0), not exit(1), because that value inside the parentheses will become the return value of your program, and 1 indicates an error occurred, but that's not the case in your program.

    The only other problem is that your calculation is out by a factor of 100. Remember 0.05 = 5%; you need to multiply a proportion by 100 to get a percentage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bad and fail of steam
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 02-19-2008, 03:07 AM
  2. Can you still view the bios screen with a bad CPU?
    By HyperCreep in forum Tech Board
    Replies: 4
    Last Post: 12-31-2006, 06:57 PM
  3. Replies: 6
    Last Post: 11-12-2005, 11:57 AM
  4. Bad coding habits
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-12-2005, 05:44 PM
  5. Shocking(kind of)
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 12-10-2002, 08:52 PM