Home > OS >  Why are my member functions/class variables outputting the wrong numbers?
Why are my member functions/class variables outputting the wrong numbers?

Time:11-09

I am learning how to use classes in c . Right now I'm working on a small program which should display the miles per gallon of a vehicle based on the given number of miles and gallons. The assignment says to call member functions within the main function in order to set the member variables within the Auto class. Here is my code:

#include <iostream>
using namespace std;

    class Auto {
        public:
            string model;
            int milesDriven;
            double gallonsOfGas;
            double calculateMilesPerGallon(int milesDriven, double gallonsOfGas) {
                return milesDriven / gallonsOfGas;
            }
            void setModel(string newModel){
                model = newModel;
            }
            void setMilesDriven(int newMiles){
                milesDriven = newMiles;
            }
            void setGallonsOfGas(double newGallons){
                gallonsOfGas = newGallons;
            }
            void output(){
                cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
                cout << "This car gets " << calculateMilesPerGallon(milesDriven, gallonsOfGas) << "mpg.";
            }
    };
    
    
    int main()
    {
        Auto modelFunction;
        Auto milesFunction;
        Auto gasFunction;
        Auto outputFunction;
        string carModel = "Toyota Camry";
        int carMiles = 100;
        double carGallons = 10;
        
        modelFunction.setModel(carModel);
        milesFunction.setMilesDriven(carMiles);
        gasFunction.setGallonsOfGas(carGallons);
        outputFunction.output();
        
        return 0;
    }

It's supposed to display something like "A Toyota Camry was driven 100, and used 10 gallons of gas, and gets 10 mpg." Instead, my output shows "A was driven -1538932792 miles, and used 4.66265e-310 This car gets -infmpg." What am I doing that is causing the output to be like this? I just started using classes so I don't have much experience with them. Thanks for the advice.

CodePudding user response:

The error you're encountering is because you're creating four different 'Auto' objects, each of which will have their own member variables. If you change your main function to the following, it will work:

int main()
{
    Auto car;
    string carModel = "Toyota Camry";
    int carMiles = 100;
    double carGallons = 10;

    car.setModel(carModel);
    car.setMilesDriven(carMiles);
    car.setGallonsOfGas(carGallons);
    car.output();

    return 0;
}

Note that there is now only one 'Auto' object entitled 'car'.

CodePudding user response:

You have to create only one instance of class Auto cause you only want to show the info for one car (with the name "Toyota Camry").

#include <iostream>

using namespace std; // strongly encouraging you to avoid using this
                     // in file scope

class Auto {
    public:
        string model;
        int milesDriven;
        double gallonsOfGas;
        double calculateMilesPerGallon(/*no params required*/) {
            return milesDriven / gallonsOfGas;
        }
        void setModel(string newModel){
            model = newModel;
        }
        void setMilesDriven(int newMiles){
            milesDriven = newMiles;
        }
        void setGallonsOfGas(double newGallons){
            gallonsOfGas = newGallons;
        }
        void output(){
            cout << "A " << model << " was driven " << milesDriven << " miles, and used " << gallonsOfGas << endl;
            cout << "This car gets " << calculateMilesPerGallon() << "mpg.";
        }
};
    
    
int main()
{
    string carModel = "Toyota Camry";
    int carMiles = 100;
    double carGallons = 10;

    Auto car;
    car.setModel(carModel);
    car.setMilesDriven(carMiles);
    car.setGallonsOfGas(carGallons);
    car.output();

    return 0;
}
  • Related