I have this program, I went all over the place with it and I got confused. Its supposed to call the accelerate function 5 times and get the current speed of the car and then display it. Then, its supposed to call the brake function 5 times and then get the current speed of the car and then display it....Could someone fix it for me?

//


#include<iostream>
#include<cstring>
using namespace std;
//
class car
{
private:
int yearModel;
int speed;
string make;
public:
car(int, string);
string getMake();
int getModel();
int getSpeed();
int accelerate();
int brake()
int make();
};
car::car(int yearModelin, string makein)
{
yearModel= yearModelin;
make= makein;
speed=0;
}
string car::getMake()
{
return make;
}
int car::getModel()
{
return yearModel;
}
int car::getSpeed();
{
return speed;
}
int car::accelerate();
{
speed= speed+5;
}
int car::brake();
{
speed=speed-5;
}
}


int main()
{
car, car1 (1999, Honda);

for (i=0; i<5; i++)
{
car.accelerate();
cout<<car1.getSpeed();
}
return 0;
}

Try changing:

car, car1 (1999, Honda);

to

car car1 (1999, "Honda");

There should be no comma between the variable's type and the variable's name. Strings should be surrounded by quotes.

you're right, i mean that was a minor mistake that I would have found out about sooner or later... what i mean about fixing are those mistakes that are keeping me from find out the answer. Every time I run this program, i have many errors and I dont understand where they're coming from...

you're right, i mean that was a minor mistake that I would have found out about sooner or later... what i mean about fixing are those mistakes that are keeping me from find out the answer. Every time I run this program, i have many errors and I dont understand where they're coming from...

Always post the error messages and the line it comes from. In this case, I'm sure it is this:

car.accelerate ();

car is the class. You don't want to accelerate the class, you want to accelerate an instance of the class. car1 is an instance of the car class:

car1.accelerate ();

You also have functions that claim to return integers, but don't. Make them void functions or make them actually return something.

ok I changed many things, but other problems rose... constructor shouls assign 0 to all speed member variables, but it doesnt...my results are wrong now....What is wrong this time?

#include<iostream>
#include<string>

using namespace std;

class car{

private:
int yearModel;
int speed;
string make;
public:
car(int, string, int);
string getMake();
int getModel();
int getSpeed();
void accelerate();
void brake();
};

car::car(int yearModelin, string makein, int speed)
{
yearModel = yearModelin;
make = makein;
speed = 0;
}

string car::getMake(){
return make;
}

int car::getModel(){
return yearModel;
}
int car::getSpeed(){
return speed;
}
void car::accelerate(){
speed= speed+5;
}

void car::brake(){
speed = speed-5;
}

int main(){

int speed = 115;
cout<<"Initial speed is set to "<<speed<<" by the user "<<endl<<endl;
car car1 (1999, "Honda", speed);

cout<<"Acceleration "<<endl;
for (int i = 0; i < 5; i++){
car1.accelerate();
cout<<car1.getSpeed()<<endl;
}

cout<<endl<<"Brake"<<endl;
for (int i = 0; i < 5; i++){
car1.brake();
cout<<car1.getSpeed()<<endl;;
}

return 0;
}

car::car(int yearModelin, string makein, int speed)
{
yearModel = yearModelin;
make = makein;
speed = 0;
}

Your local variable and your class data member have the same name (speed). To avoid confusion, make them different.

car::car(int yearModelin, string makein, int spd)
{
yearModel = yearModelin;
make = makein;
speed = spd;
}

Also, why would you pass a constructor a variable for speed and then simply assign speed to equal 0? If you want to assign speed to 0, there's no need for the constructor to take a speed parameter. Do this:

car::car(int yearModelin, string makein)
{
yearModel = yearModelin;
make = makein;
speed = 0;
}

which is what you originally did.

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.