Car program

Pages: 12
I'm getting this error. I think it has something to do with how I'm calling the member functions brake and accelerate.
http://prntscr.com/bzvjho

I have to design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method I have to get the current speed of the car and display it. Then I have to call the brake method five times and after each call the the brake method get the current speed of the car and display it.

I'm trying to get an output of:

Enter the car model's year: 1957
Enter the car's make: Chevy
The model year is 1957
The make is Chevy
The speed is 0

Let's see what it can do!!
The speed is,...
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
STOP! STOP! Let me OUT!
The speed is,...
145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0

(I included comments in my code)

This is what I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "Car header.h"

using namespace std;

const int NUM_FOR_INCREASE=150;
const int NUM_FOR_DECREASE=0;
const int NUM_FOR_CAL=5;
int main()
{
    int yearModel;
    string make;
    int speed=0;

    cout << "Enter the car model year" << endl;
    cin >> yearModel;
    cout << "Enter the cars make" << endl;
    cin >> make;
    cout << "The model year is " << yearModel << endl;
    cout << "The make is " << make << endl;
    cout << "The speed is " << speed << endl;
    cout << "Let's see what it can do." << endl;
    cout << "The speed is,..." << endl;

    Car carObject();

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
        speed=speed+NUM_FOR_CAL
        cout << speed << endl;//I'm not sure how to display the speed each time horizontally  like I want in the output example
        cout << "Stop! Stop! Let me out." << endl;

}

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
                                                                                                    //150 now
        speed=speed-NUM_FOR_CAL
        cout << speed << endl; //I'm not sure how to display the speed each time horizontally like I want in the output example
        cout << "I'll walk from here." << endl;

}

}


Last edited on
closed account (21vXjE8b)
Line 31/41 should be cout << speed << " ";

Then, put cout << "Stop! Stop! Let me out." << endl; in line 35 (after the loop) and cout << "I'll walk from here." << endl; in line 45 (after the loop, too).

Then, line 6 should be const int NUM_FOR_INCREASE=80; to get speed ranging from 5 to 150. With const int NUM_FOR_INCREASE=150; you'd get the speed ranging from 5 to 500!
Oh. I didn't even know I had to correct those. Thanks for your help!

Do you have any idea how to fix the errors in my screenshot and get the numbers in accelerate and brake to show horizontally?

I wouldn't want it showing like
5
10
15
20
etc.
closed account (21vXjE8b)
Try changing Car carObject(); to Car carObject;
Also, if you change the line 31/41 you will get the numbers showed horizontally.
Last edited on
Changing it to Car carObject; won't work since its a member function I'm trying to call.
closed account (21vXjE8b)
carObject(); is a function? So... what carObject.accelerate() and carObject.brake() are supposed to be?
Last edited on
CarObject is supposed to merely be a Car object.

The other two are supposed to be member functions that are called so I can use them in my code.

Though I may have done something wrong there.
Last edited on
closed account (21vXjE8b)
So, if you only want to declare an object called carObject it should be Car carObject;

If you put Car carObject(); you are declaring the function carObject().

carObject.accelerate() and carObject.brake() are useless (I think, since I don't have the entire code). You already did the acceleration loop and brake loop inside main()

It would be a good idea to do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Car
{
   public:
      // Line 27 to 34 would be the accelerate() function in here
      // Line 36 to 44 would be the brake() function in here
}

int main()
{
   // Code till line 23 goes here
   Car carObject;
   carObject.accelerate();
   carObject.brake();
}

Last edited on
Ah then my member functions should work?
closed account (21vXjE8b)
Yeah, they should work ^^
Well, I tried that, but it actually gave me more errors than I had before haha.
http://prntscr.com/bzxhzx

Well, if you read my OP, I need to call the accelerate and brake method from my header file into my main to use it. Did you see how I included a header in the top? That header has my class.

I'm trying to use the methods in them for my main program.

This is the full code (not including my header since I had no problem including it in my main program).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include "Car header.h"

using namespace std;

const int NUM_FOR_INCREASE=80;
const int NUM_FOR_DECREASE=0;
const int NUM_FOR_CAL=5;
int main()
{
    int yearModel;
    string make;
    int speed=0;

    cout << "Enter the car model year" << endl;
    cin >> yearModel;
    cout << "Enter the cars make" << endl;
    cin >> make;
    cout << "The model year is " << yearModel << endl;
    cout << "The make is " << make << endl;
    cout << "The speed is " << speed << endl;
    cout << "Let's see what it can do." << endl;
    cout << "The speed is,..." << endl;

    Car carObject;

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
        speed=speed+NUM_FOR_CAL;
        cout << speed << " ";//I'm not sure how to display the speed each time horizontally  like I want in the output example

}
    cout << "Stop! Stop! Let me out." << endl;

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
                                                                                                    //150 now
        speed=speed-NUM_FOR_CAL;
        cout << speed << " "; //I'm not sure how to display the speed each time horizontally like I want in the output example
}
    cout << "I'll walk from here." << endl;

}
Last edited on
closed account (21vXjE8b)
Well, for the 2 last errors, just add a semi-collon at the end of line 29 and 38.
But the other error... Hmm... I don't really know what's going on =S
It's displayed with the last code you posted?
I fixed those haha.

Here, maybe this will help.

This is the header file I'm using the contains my class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef CAR_HEADER_H_INCLUDED
#define CAR_HEADER_H_INCLUDED
#include <string>

using namespace std;

class Car
{
private:
    //private fields to hold data
    int yearModel;
    string make;
    int speed;

    //constructor
public:

    Car(int ym, string mk)
    {
        yearModel=ym;
        make=mk;
        speed=0;
    }

    void setYearModel(int ym)
    {
        yearModel=ym;
    }

    void setMake (string mk)
    {
        make=mk;
    }

    int returnYearModel()
    {
        return yearModel;
    }

    string returnMake()
    {
        return make;
    }
    int returnSpeed()
    {
        return speed;
    }

    void accelerate()
    {
        speed += 5;
    }

    void brake()
    {
        speed -= 5;
    }
};
#endif // CAR_HEADER_H_INCLUDED 


This works fine.

This is the program where I included my class which is in my header. My headers name (which has my class in it) is named Car header.h.

I managed to include Car header.h just fine.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include "Car header.h"

using namespace std;

const int NUM_FOR_INCREASE=80;
const int NUM_FOR_DECREASE=0;
const int NUM_FOR_CAL=5;
int main()
{
    int yearModel;
    string make;
    int speed=0;

    cout << "Enter the car model year" << endl;
    cin >> yearModel;
    cout << "Enter the cars make" << endl;
    cin >> make;
    cout << "The model year is " << yearModel << endl;
    cout << "The make is " << make << endl;
    cout << "The speed is " << speed << endl;
    cout << "Let's see what it can do." << endl;
    cout << "The speed is,..." << endl;

    Car carObject;

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
        speed=speed+NUM_FOR_CAL;
        cout << speed << " ";//I'm not sure how to display the speed each time horizontally  like I want in the output example

}
    cout << "Stop! Stop! Let me out." << endl;

for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
        carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
                                                                                                    //150 now
        speed=speed-NUM_FOR_CAL;
        cout << speed << " "; //I'm not sure how to display the speed each time horizontally like I want in the output example
}
    cout << "I'll walk from here." << endl;

}


What I'm trying to do is use the accelerate and brake function from the header in my main to do this:

I have to design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method I have to get the current speed of the car and display it. Then I have to call the brake method five times and after each call the the brake method get the current speed of the car and display it.


Last edited on
closed account (21vXjE8b)
Well, you have a constructor Car(int ym, string mk) with 2 arguments and in line 25 in main() you wanna declare an object, but it's missing the arguments.
Last edited on
Adding the () put it back at these two errors. xD

http://prntscr.com/bzyc0j
closed account (21vXjE8b)
In line 25 put Car carObject(yearModel, make);
Alright, got everything fixed for now. (Stay here though lol oh and turn on your PM)

I'm curious, why did I have to put yearModel, make in that if I'm not going to be using it?

I mean, I used it before the car object, but why does it have to be in the parentheses in the car object if I'm not going to use it after all that?
Last edited on
Oh wow it actually worked.

http://prntscr.com/bzyk2f

This is the output.


I just need to put the

cout << "Stop! Stop! Let me out." << endl;
cout << "I'll walk from here." << endl;

On separate lines when the program is run.

I also need it to start going down starting at 150.

I tried to change it to five, but that didn't work.


closed account (21vXjE8b)
Because when you created the constructor Car, you assumed it would carry 2 parameters --> an int and a string.
If you want to declare an object with that constructor (Car with parameters), you HAVE to put the 2 parameters when declaring an object in main().
But I agree, this is a little bit strange... Another way to do it, is to create a constructor Car with no arguments.

To put in separate lines just use \n before the message in cout.

To start in 150, just alter the code to this:
1
2
3
4
5
6
7
8
9
for(int n = 50; n < NUM_FOR_INCREASE + 1; ++n)
{
        carObject.brake();
		
        cout << speed << " ";    
                                                                                               
        speed = speed - NUM_FOR_CAL; 
}
    cout << "I'll walk from here." << endl;
Last edited on
Well thanks for all the help! I can't thank you enough (as I already did a million times in PM lol).

: )
Pages: 12