Hey guys! I have a class program here that I am stuck on. This is my first time officially asking for help, but after studying all avenues and research I still find myself wracking my brain. Most of the code is complete and without error...except for the last bit...of course. Here are the parameters of the assignment:

Writing Assignment: Class Car

Implement a class Car with the following properties. A car has a certain fuel efficiency (measured in miles/gallon or liters/km—pick one) and a certain amount of fuel in the gas tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Supply a function drive that simulates driving the car for a certain distance, reducing the fuel level in the gas tank, and functions get_gas, to return the current fuel level, and add_gas, to tank up. Sample usage:

Car my_beemer(29); // 29 miles per gallon

my_beemer.add_gas(20); // Tank 20 gallons

my_beemer.drive(100); // Drive 100 miles

cout << my_beemer.get_gas() << "\n"; // Print fuel remaining

Where I am having trouble is the function drive that simulates driving for a specified distance. This will reduce the fuel level and the function get_gas, and return back to the current fuel level. I have tried coding this multiple ways and the compiler will typically deny just one word rendering all of it useless.....so.....I'm tired, and without chocolate on valentines day and my husband is working the dinner shift. Please please somebody help me, so I can enjoy the evening with him when he comes home. Hopefully with chocolate in his hand.

This is what I have so far:
(by the way...line 33 is just hanging there. I had a thought and just left it there. Maybe it will prove useful...maybe it wont)

#include <iostream>
#include <string>

using namespace std;

class Car
`Inline Code Example Here`
private:
    double mpg; // Fuel efficiency in miles per gallon //
    double fuel_capacity; // maximum fuel level in tank //
    double fuelLevel; // amount of fuel in tank //

public:
    Car (double mpg, double capacity); // Car is object. double mpg (efficiency) and capacity are constructors //
    double get_gas(); // will return the current fuel level //
    void add_gas (double gallons); // Tank Up! //
    void distance (double milesDriven); // distance of miles driven //

};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){  // Calling Constructor from //
                                                                           // Constructor              //
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

void Car::add_gas (double gallons){      // If gas in the tank is below the fuel capacity tank up //
if ((fuelLevel + gallons) < fuel_capacity)
    fuelLevel += gallons;

}
void Car:: get_gas.distance (milesDriven){

}
}

Recommended Answers

All 12 Replies

The first thing I see is syntax errors. You have no open brace for the class prototype and there is an extra closing brace at the end of the code. Also Get_Gas doesn't follow the prototype declaration.

Sorry about that. I had a little trouble getting it to copy and paste and found some extra line of inserted code placed in my code after 'class Car'. After I finally got my code to insert, I found the line and deleted it, but I guess I accidentally threw away the opening brace with it.

Here is my original code. Also...the code wasn't complete. That is why the extra closing brace was there. There was supoosed to be more code written in that spot. I keep coding different avenues and deleted them when they didn't work.

The get_gas is what I am trying to get to work. What I had written there was just an idea I was playing at. Trying to get the brain to click on something. I deleted that line out. I still need to compile the get_gas function though.

I have already declared the get_gas function and the distance function. I am just trying to get them to work. I have been working on it since last night and I cannot simply get it to work. Here is my code again. But the part I am stuck on is not in there. I have tried

void distance (milesDriven){
cout << "How many miles have you driven: ";
cin >> milesDriven;
fuelLevel -=miles/mpg;
}

But this wont work. This seems to be the most logical, but It always reads an error. Either it will tell me milesDriven isn't declared in the scope or mpg is in the scope. When I change it to miles the compiler will tell me that miles isn't in the scope.

#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    double mpg; // Fuel efficiency in miles per gallon //
    double fuel_capacity; // maximum fuel level in tank //
    double fuelLevel; // amount of fuel in tank //

public:
    Car (double mpg, double capacity); // Car is object. double mpg (efficiency) and capacity are constructors //
    double get_gas(); // will return the current fuel level //
    void add_gas (double gallons); // Tank Up! //
    void distance (double milesDriven); // distance of miles driven //

};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){  // Calling Constructor from //
                                                                           // Constructor              //
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

void Car::add_gas (double gallons){      // If gas in the tank is below the fuel capacity tank up //
if ((fuelLevel + gallons) < fuel_capacity)
    fuelLevel += gallons;

}

void distance (miles){
    cout << "How many miles have you driven: ";
    cin >> miles;
    fuelLevel -=miles/mpg;
    }

In both the distance( ) functions above, you pass in a miles(driven) paramenter, then do an input action to that value. The general concept should be that the user of an instance of your car supplies that value to the car object, through that parameter, and the car just makes the change. Like this:

void distance (miles){
    fuelLevel -= miles/mpg;
    }

You might want to put a check in there that the fuel level doesn't go below 0.

I don't think the name of that function really makes sense in the context of the car. It's more like "consume_fuel".

You have 1 syntax problem and a few redundancy problems. First lets tackle the syntax problem so that you can get your code to run.

The syntax error is that void distance(miles) is not valid C++ code. When you have parameters in a function it must have a type. What you want is void distance(double miles) which is exactly what you wrote in the class description.

This leads to the next issue, namely that you are going to end up asknig the user for the miles variable twice. Instead you want to pass miles into the function, like vmanes said. Thus your fixed distance function is:

void distance(double miles) {
    fuelLevel-=miles/mpg;
}

Finally, there is a redundancy in your constructor. While it works fine, it is very silly. Here is your constructor:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

Which is essentially the same as:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
//:mpg(mpg) becomes:
mpg = mpg;
//:fuel_capacity(capacity) becames:
fuel_capacity = capacity;
//now the next two lines are redundant:
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

A better constructor is:

Car::Car(double mpg, double capacity):  mpg(mpg), 
                                        fuel_capacity(capacity),
                                        fuelLevel(0.0)//you can set these to values too
                                        {/*no function body at all!*/}

Hope this helps!

You have 1 syntax problem and a few redundancy problems. First lets tackle the syntax problem so that you can get your code to run.

The syntax error is that void distance(miles) is not valid C++ code. When you have parameters in a function it must have a type. What you want is void distance(double miles) which is exactly what you wrote in the class description.

This leads to the next issue, namely that you are going to end up asknig the user for the miles variable twice. Instead you want to pass miles into the function, like vmanes said. Thus your fixed distance function is:

void distance(double miles) {
    fuelLevel-=miles/mpg;
}

Finally, there is a redundancy in your constructor. While it works fine, it is very silly. Here is your constructor:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

Which is essentially the same as:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){
//:mpg(mpg) becomes:
mpg = mpg;
//:fuel_capacity(capacity) becames:
fuel_capacity = capacity;
//now the next two lines are redundant:
mpg = mpg;
fuel_capacity = capacity;
fuelLevel = 0.0;
}

A better constructor is:

Car::Car(double mpg, double capacity):  mpg(mpg), 
                                        fuel_capacity(capacity),
                                        fuelLevel(0.0)//you can set these to values too
                                        {/*no function body at all!*/}

Hope this helps!

Thanks for the help guys. @ Labdabeta I did attempt rewriting the constructor but my compiler was not happy with it, so I went back it kept what I had. It is a perfect example for calling constructors in the book, so hopefully my professor will appreaciate it. And my bad on forgetting to call 'double' on the mpg. Tired eyes sometimes need new ones to spot simple syntax errors. Chocolate and energy drinks also helps but I had neither for a few days due to being snowed in a couple days, but my husband came home with tons of it! :) I have not fully finished the program yet. I just have to tackle the output and returning the get_gas. If I have anymore questions I will let ya know. ;) Thanks!

Alright. I am done coding. Everything is complete and without error...except...it won't build. Another set of eyes to see what can remedy this would be great. Ready to just submit as is...and I hate not completing work...but I have tons of other homework to get to, so I just may have to cut my losses move on. Here is the code.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Car
{

public:
    Car (double mpg, double capacity);     /* Car is object. double mpg (efficiency) and capacity are constructors */
    double get_gas();                     /* will return the current fuel level */
    void add_gas (double gallons);       /* Tank Up! */
    void distance (double miles);       /* distance of miles driven */

private:
    double mpg;              /* Fuel efficiency in miles per gallon */
    double fuel_capacity;   /* maximum fuel level in tank */
    double fuelLevel;      /* amount of fuel in tank */
    double milesDriven;
};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){  /* Calling Constructor from  */
                                                                          /*Constructor. Setting their */
mpg = 0.0;                                                               /* value at 0.0              */
fuel_capacity = 0.0;
fuelLevel = 0.0;
}

void distance (double miles, double mpg, double fuelLevel){       /* Drive the car for a specified     */
fuelLevel -= miles/mpg;                                          /* distance in miles. Subtract  the  */
cout << " Enter the amount of fuel in gas tank: " << endl;      /*amount of fuel by miles and divide */
cin >> fuelLevel;                                              /* by mpg.                           */
cout << " Enter the fuel efficiency in miles per gallon: " << endl;
cin >> mpg;
cout << " Enter distance driven in miles: " << endl;
cin >> miles;
}

void Car::add_gas (double gallons){                /* If gas in the tank is below the fuel capacity tank up */
if ((fuelLevel + gallons) < fuel_capacity)
    fuelLevel += gallons;
}

double get_gas(double fuelLevel){
cout << " What is the current fuel level: " << endl;
cin >> fuelLevel;
return 0;
}

That code is a mess! It didn't transfer too well. I have fixed my comments in my code to make it work better with the code block and more readable.
The clean code is below.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Car
{
/* Car is object. double mpg (efficiency) and capacity are constructors */
/* get_gas will return the current fuel level */
/* add_gas will prompt you to Tank Up! */
/* distance of miles driven */
public:
    Car (double mpg, double capacity);
    double get_gas();
    void add_gas (double gallons);
    void distance (double miles);

/* mpg = Fuel efficiency in miles per gallon */
/* fuel capacity = maximum fuel level in tank */
/* fuelLevel = amount of fuel in tank */
private:
    double mpg;
    double fuel_capacity;
    double fuelLevel;
    double milesDriven;
};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){

 /* Calling Constructor from  */
/*Constructor. Setting their */
/* Setting the value at 0.0*/

mpg = 0.0;
fuel_capacity = 0.0;
fuelLevel = 0.0;
}

/* Drive the car for a specified     */
/* distance in miles. Subtract  the  */
/*amount of fuel by miles and divide */
/* by mpg.                           */
void distance (double miles, double mpg, double fuelLevel){
fuelLevel -= miles/mpg;
cout << " Enter the amount of fuel in gas tank: " << endl;
cin >> fuelLevel;
cout << " Enter the fuel efficiency in miles per gallon: " << endl;
cin >> mpg;
cout << " Enter distance driven in miles: " << endl;
cin >> miles;
}

/* If gas in the tank is below the fuel capacity tank up */
void Car::add_gas (double gallons){
if ((fuelLevel + gallons) < fuel_capacity)
    fuelLevel += gallons;
}

double get_gas(double fuelLevel){
cout << " What is the current fuel level: " << endl;
cin >> fuelLevel;
return 0;
}
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

class Car
{
/* Car is object. double mpg (efficiency) and capacity are constructors */
/* get_gas will return the current fuel level */
/* add_gas will prompt you to Tank Up! */
/* distance of miles driven */
public:
    Car (double mpg, double capacity);
    double get_gas();
    void add_gas (double gallons);
    void distance (double miles);

/* mpg = Fuel efficiency in miles per gallon */
/* fuel capacity = maximum fuel level in tank */
/* fuelLevel = amount of fuel in tank */
private:
    double mpg;
    double fuel_capacity;
    double fuelLevel;
    double milesDriven;
};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity){

 /* Calling Constructor from  */
/*Constructor. Setting their */
/* Setting the value at 0.0*/

mpg = 0.0;
fuel_capacity = 0.0;
fuelLevel = 0.0;
}

/* Drive the car for a specified     */
/* distance in miles. Subtract  the  */
/*amount of fuel by miles and divide */
/* by mpg.                           */
void distance (double miles, double mpg, double fuelLevel){
fuelLevel -= miles/mpg;
cout << " Enter the amount of fuel in gas tank: " << endl;
cin >> fuelLevel;
cout << " Enter the fuel efficiency in miles per gallon: " << endl;
cin >> mpg;
cout << " Enter distance driven in miles: " << endl;
cin >> miles;
}

/* If gas in the tank is below the fuel capacity tank up */
void Car::add_gas (double gallons){
if ((fuelLevel + gallons) < fuel_capacity)
    fuelLevel += gallons;
}

double get_gas(double fuelLevel){
cout << " What is the current fuel level: " << endl;
cin >> fuelLevel;
return 0;
}

This NOT good!
This is better:

class car {
.
.
.
public:
    car(double mpg = 0.0, double capacity = 0.0);
.
.
.   
};

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity)
{}

The way you did it, if someone instantiated a car type with something other than 0.0 for mpg and capacity, they would be fubar'd! Also, inside the constructor, you want to verify that the values passed are NOT less-than-zero. IE:

Car::Car(double mpg, double capacity): mpg(mpg), fuel_capacity(capacity)
{
    if (mpg < 0.0 || capacity < 0.0)
    {
        // Throw an exception - and remember to tag the
        // the declaration of the car constructor in the header
        // to indicate it may throw this exception.
    }
}

Note that I did not add the throw tag to the declaration - I am leaving that to you. What it is depends upon the exception type you decide to throw. :-)

Ok I am new to C++ and I am trying learn at my self pace and when I try to place this in my VS 2010 I keep getting an error at the very start.

#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <conio.h>

using namespace System;
using namespace std;

namespace ClassCar {
 public ref class Class1
    {
        // TODO: Add your methods for this class here.
             Car (double mpg, double capacity); // error is here I think
             double get_gas();
             void add_gas (double gallons);
             void distance (double miles);
    };
    };



// I get this error
// error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I know I am really new and slow to this but I can't even get out of the gate here to learn. Can someone tell me what I am missing? I have Google this for over an hour and I am still lost.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.