Car

Okay, here's a program i made and I know it has 2 errors at the bottom. with the accelerate and brake. But what i need it to do is the accelerate function should add 5 to the speed member variable each time it is called and the brake function should subtract 5 from the speed member variable each time it is called. So does that mean i have to put a loop?

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstring>
#include <cctype>
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();
void displayMenu();
};
 
Car::Car(int YearofModel, string Makeby, int Spd)
{
YearModel = YearofModel;
Make = Makeby;
Speed = Spd;
}
//To get who makes the car.
string Car::getMake()
{
return Make;
}
//To get the year of the car.
int Car::getModel()
{
return YearModel;
}
//To holds the car actual speed.
int Car::getSpeed()
{
return Speed;
}
//To increase speed by 5.
void Car::Accelerate()
{
Speed = Speed + 5;
}
//To drop the speed of the car by 5.
void Car::Brake()
{
Speed = Speed - 5;
}
 
void displayMenu()
{
cout <<"\n Menu\n";
cout << "----------------------------\n";
cout << "A)Accelerate the Car\n";
cout << "B)Push the Brake on the Car\n";
cout << "C)Exit the program\n\n";
cout << "Enter your choice: ";
}
 
int main()
{
int Speed = 0; //Start Cars speed at zero.
char choice; //Menu selection
 
cout << "The speed of the SUV is set to: " << Speed <<endl;
Car first( 2003, "Hyundai", Speed);
 
//Display the menu and get a valid selection
do
{
displayMenu();
cin >> choice;
while(toupper(choice) < 'A' || toupper(choice) > 'C')
{
cout << "Please make a choice of A or B or C:";
cin >> choice;
}
 
//Process the user's menu selection
{
switch (choice)
{
case 'a':
case 'A': cout << "You are accelerating the car. ";
cout << Accelerate(first) << endl;
break;
case 'b':
case 'B': cout << "You have choosen to push the brake.";
cout << Brake(first) << endl;
break;}
}
}while (toupper(choice) != 'C');
 
return 0;
} 
Last edited on
closed account (D80DSL3A)
Not sure what you mean. The Brake and Accelerate functions already are in a loop.

Re. the errors.You need to call the functions on an instance of the class.
on lines 89 and 93 it should be first.Brake() and first.Accelerate() respectively.
You will then find the next errors. These will be caused by Brake() and Accelerate being void functions. There is nothing to cout. Perhaps these functions should return the new value of Speed.
So on lines 44 and 49, I would need to change them to int's so they can return the value entered?
closed account (D80DSL3A)
Yes. Also, add the line return Speed; to these functions and change the return type to int for the prototypes as well (lines 17 and 18).

Hopefully then the program will run so you can see if it does what you want it to do.
I changed the the voids into int's but its still not reading it. I think i have the call functions wrong

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
 
class Car
{
private:
int YearModel;
int Speed;
string Make;
public:
Car(int, string, int);
string getMake();
int getModel();
int getSpeed();
int Accelerate();
int Brake();
void displayMenu();
};
 
Car::Car(int YearofModel, string Makeby, int Spd)
{
YearModel = YearofModel;
Make = Makeby;
Speed = Spd;
}
//To get who makes the car.
string Car::getMake()
{
return Make;
}
//To get the year of the car.
int Car::getModel()
{
return YearModel;
}
//To holds the car actual speed.
int Car::getSpeed()
{
return Speed;
}
//To increase speed by 5.
int Car::Accelerate()
{
Speed = Speed + 5;
return Speed;
}
//To drop the speed of the car by 5.
int Car::Brake()
{
Speed = Speed - 5;
return Speed;
}
 
void displayMenu()
{
cout <<"\n Menu\n";
cout << "----------------------------\n";
cout << "A)Accelerate the Car\n";
cout << "B)Push the Brake on the Car\n";
cout << "C)Exit the program\n\n";
cout << "Enter your choice: ";
}
 
int main()
{
int Speed = 0; //Start Cars speed at zero.
char choice; //Menu selection
 
cout << "The speed of the SUV is set to: " << Speed <<endl;
Car first( 2003, "Hyundai", Speed);
 
//Display the menu and get a valid selection
do
{
displayMenu();
cin >> choice;
while(toupper(choice) < 'A' || toupper(choice) > 'C')
{
cout << "Please make a choice of A or B or C:";
cin >> choice;
}
 
//Process the user's menu selection
{
switch (choice)
{
case 'a':
case 'A': cout << "You are accelerating the car.";
cout << (first).Accelerate << endl;
break;
case 'b':
case 'B': cout << "You have choosen to push the brake.";
cout << (first).Brake << endl;
break;}
}
}while (toupper(choice) != 'C');
 
return 0;
} 
closed account (D80DSL3A)
Please read the posts in your thread.
From 2 posts back:
it should be first.Brake() and first.Accelerate()


There are some tutorials on this site if you don't have a book to read about classes.
see: http://www.cplusplus.com/doc/tutorial/classes/

I got it to work, and thank you for you help. but just one more thing.

On the instructions it says to demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then call the brake function five times. After each call to the brake function get the current speed of the car and display it.

Did I do this right? because I asked the user to enter whether or not to accelerate or brake the car. Or I did it right and I'm supposed to enter accelerate 5 times and then brake 5 times.

And sorry, I was wondering why it add .5 instead of 5 when the initial speed is set to 0
closed account (D80DSL3A)
You're welcome.
I think the instructions mean that you should not be prompting the user for choices.
You should:
1) Create a car object - check!
2) Call the accelerate function 5 times - a for loop would work well for this.
3) Following each call to Accelerate: get the current speed (use the getSpeed() function for this )
and display it.
In a for loop then, you want to do the following:
1
2
first.Accelerate();// I guess you could change the return type back to void
cout << first.getSpeed() << endl;


4) Repeat steps 2, 3 above using the Brake() instead of the Accelerate().

I have no idea about the .5 thing. There isn't a float or double variable anywhere.
I figured out what the .5 was. I forgot i had put a period at the end of the sentence, so I thought it was .5

and for the for loop I would put this?
1
2
3
4
5
{
  int i;
  for(i= 0; i < 5; i++)
    cout << i << endl;
}


closed account (D80DSL3A)
Not quite.

fun2code wrote:

In a for loop then, you want to do the following:
1
2
first.Accelerate();// I guess you could change the return type back to void
cout << first.getSpeed() << endl;



I'm sorry that I am unable to explain what to do in an understandable manner.
Hopefully someone else can explain it better.
Good luck with your homework.
Topic archived. No new replies allowed.