Car Class

This is my code

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
102
103
104
105
#include "stdafx.h"
#include <iostream>
#include <string>
#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;
}

string Car::getMake()
{
	return Make;
}

int Car::getModel()
{
	return YearModel;
}

int Car::getSpeed()
{
	return Speed;
}

int Car::Accelerate()
{
	Speed = Speed + 5;

	return Speed;
}

int Car::Brake() 
{
	Speed = Speed - 5;

	return Speed;
}

void displayMenu()
{
	cout <<"\n            Car Menu          " << endl;
	cout << "-------------------------------" << endl;
	cout << "A) Accelerate the Car" << endl;
	cout << "B) Push the Brake on the Car" << endl;
	cout << "C) Exit the program" << endl;
	cout << "\nEnter your choice: ";
}

int main()
{
	int Speed = 0; 
	char choice; 

	cout << "The speed of the Ferrari is set to: " << Speed <<endl;
	Car first(2012, "Ferrari 458 Spider", Speed);
	
	do
	{
		displayMenu();
		cin >> choice;
		while(toupper(choice) < 'A' || toupper(choice) > 'C')
		{
			cout << "Please make a choice of A or B or C:";
			cin >> choice;
		}
		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');

	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

	return 0;
}


How can i make this program accept a users input for the make, yearmodel.
You can cin to temp variables in main then pass those to the car constructor:
1
2
3
4
5
6
cout << "Enter the make: ";
cin >> tempMake;

// do same for tempYearmodel

Car test(tempYearModel, tempMake, 0);


This will create a car object with the user inputted yearmodel and make, and 0 speed.
This is my updated code, i changed it around a little.

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Reaper

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int exit();

class Car
{
	private:
		int Year;
		string Make;
		int Speed;

	public:
		Car(int, string, int);
		int getYear();
		string getMake();
		int getSpeed();
		int Accelerate();
		void getDisplay();
		int Brake();
};

Car::Car(int Yr, string Mk, int Spd)
{
	Year = Yr;
	Make = Mk;
	Speed = Spd;
}

int Car::getYear()
{
	cout << "Enter the year of the car: ";
	cin >> Year;

	return Year;
}

string Car::getMake()
{
	cout << "Enter the make and model of the car: ";
	cin >> Make;

	return Make;
}

int Car::getSpeed()
{
	cout << "Enter the speed of the car: ";
	cin >> Speed;

	return Speed;
}

int Car::Accelerate()
{
	Speed = Speed + 5;

	return Speed;
}

int Car::Brake()
{
	Speed = Speed - 5;

	return Speed;
}

void getDisplay()
{
	int choice;
	
        cout << "The car is a " Car::getYear() << " " << Car::getMake << " going " << Car::getSpeed() << " MPH." << endl;
	Car car(getYear(), getMake, getSpeed());

	do
	{
		cout << "          Menu         " << endl;
		cout << "-----------------------" << endl;
		cout << " 1. Accelerate         " << endl;
		cout << " 2. Brake              " << endl;
		cout << " 3. Exit               " << endl;
		cout << "-----------------------" << endl;
		cout << "\nEnter your choice:   " << endl;
		cin >> choice;

		switch (choice)
		{
			case 1: cout << "Accelerating";
					cout << car.Accelerate();
					break;
			case 2: cout << "Braking";
					cout << car.Brake();
					break;
			case 3: cout << "Exiting Program";
					exit();
					break;
		}

		while (choice < 1 || choice > 3)
		{
			cout << "\nYour choice must be 1-3. Re-enter.\n" << endl;
			cout << "          Menu         " << endl;
			cout << "-----------------------" << endl;
			cout << " 1. Accelerate         " << endl;
			cout << " 2. Brake              " << endl;
			cout << " 3. Exit               " << endl;
			cout << "-----------------------" << endl;
			cout << "\nEnter your choice:   " << endl;
			cin >> choice;
		}
	} while (choice != 3);
}

int exit()
{
	return (0);
}

int main()
{
	getDisplay();

	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

	return 0;
}


My errors are line 76 and 77, i cant get them to register the input for year, make and speed.
Last edited on
However, if i can only do it by making a temp where would i define the temp variables and put them.
Change the header of getDisplay to:
 
void Car::getDisplay()


As it is now the function isn't related to Car, so it does not know those values.
Ok heres my updated code

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Reaper

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int exit();

class Car
{
	private:
		int Year;
		string Make;
		int Speed;

	public:
		Car(int, string, int);
		int getYear();
		string getMake();
		int getSpeed();
		int Accelerate();
		void getDisplay();
		int Brake();
};

Car::Car(int Yr, string Mk, int Spd)
{
	Year = Yr;
	Make = Mk;
	Speed = Spd;
}

int Car::getYear()
{
	cout << "Enter the year of the car: ";
	cin >> Year;

	return Year;
}

string Car::getMake()
{
	cout << "Enter the make and model of the car: ";
	cin >> Make;

	return Make;
}

int Car::getSpeed()
{
	cout << "Enter the speed of the car: ";
	cin >> Speed;

	return Speed;
}

int Car::Accelerate()
{
	Speed = Speed + 5;

	return Speed;
}

int Car::Brake()
{
	Speed = Speed - 5;

	return Speed;
}

void Car::getDisplay()
{
	int choice;
	
	cout << "The car is a " << getYear() << " " << getMake() << " going " << getSpeed() << " MPH." << endl;
	Car car(getYear(), getMake(), getSpeed());

	do
	{
		cout << "          Menu         " << endl;
		cout << "-----------------------" << endl;
		cout << " 1. Accelerate         " << endl;
		cout << " 2. Brake              " << endl;
		cout << " 3. Exit               " << endl;
		cout << "-----------------------" << endl;
		cout << "\nEnter your choice:   " << endl;
		cin >> choice;

		switch (choice)
		{
			case 1: cout << "Accelerating";
					cout << car.Accelerate();
					break;
			case 2: cout << "Braking";
					cout << car.Brake();
					break;
			case 3: cout << "Exiting Program";
					exit();
					break;
		}

		while (choice < 1 || choice > 3)
		{
			cout << "\nYour choice must be 1-3. Re-enter.\n" << endl;
			cout << "          Menu         " << endl;
			cout << "-----------------------" << endl;
			cout << " 1. Accelerate         " << endl;
			cout << " 2. Brake              " << endl;
			cout << " 3. Exit               " << endl;
			cout << "-----------------------" << endl;
			cout << "\nEnter your choice:   " << endl;
			cin >> choice;
		}
	} while (choice != 3);
}

int exit()
{
	return (0);
}

int main()
{
	getDisplay();

	cout << endl << "Press ENTER to exit...";
	cin.clear();
	cin.sync();
	cin.get();

	return 0;
}


Now i get one error say getDisplay prototype not defined. Line 125
You need to create a Car object and call it using that.
You mean something like Car display;

when i try to make a object it tells me no default constructor, even if i do
Car car;
Yeah, because you never defined a default constructor. You can change your constructor to:
1
2
3
4
5
6
Car::Car(int Yr = 0, string Mk = " ", int Spd = 0)
{
	Year = Yr;
	Make = Mk;
	Speed = Spd;
}
Last edited on
oh wow i feel stupid, lol thanks freddy
Last edited on
Topic archived. No new replies allowed.