Cannot get the second half of my program to compile


Hi coders,

I have been working on this assignment all day, and the code just wont run properly. When I compile the code it prompts me to enter the inputs for the make, model, year and speed of a car... but after that the window closes, which is surprising to me since I included system("pause"). The code that isn't showing is the display of the make, model, year and speed entered by the user, and then the 3 incrementing acceleration and braking portions. Can someone tell me where I've made a mistake? And let me know if I need to describe anything better.

Thank you guys!


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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  #include <iostream>
#include <string>

using namespace std;

class Car
{
	
private:

	int year;
	string make; 
	string model;
	int speed;

public:
	
	Car ()
	{
		year = 0;
		make = "";
		model = "";
		speed = 0;
	}

	
	Car(int year, string model, string make, int speed )
	{
		setYear(year);
		setMake(make);
		setModel(model);
		setSpeed(speed);

	}


	//Set function(s)- mutators- used to provide data validation for our members
	void setYear(int y)
	{
		if (y > 1850)
		{
			year = y; 
		}
		else
		{
			year = 1850;
		}
	}

	string setModel(string model) 
	{

		return model;

	}

	string setMake(string make) 
	{

		return make;

	}

	void setSpeed(int s)
	{
		if (s > 0)
		{
			speed = s;
		}
		else
		{
			speed = 1;
		}
	}

	void accelerate()
	{
		speed += 10;
	}

	void brake()
	{
		speed -= 10;
	}
	
	//Get function(s)- accessors-used to return the values stored in the member variables
	int getYear()
	{
		return year;
	}

	string getMake() 
	{
		return make;
	}

	string getModel()
	{
		return model;
	}

	int getSpeed()
	{
		return speed;
	}

};

int main()
{
	int number;
	Car object;

	int year;
	int speed;
	string make;
	string model;

	 cout << "Car Year: ";
	 cin >> year;

	 cout << "Car Make: ";
	 cin >> make;

	 cout << "Car Model: ";
	 cin >> model;

	 cout << "Car Speed: ";
	 cin >> speed;

	 object.setYear(year);
	 object.setMake(make);
	 object.setModel(model);
	 object.setMake(make);

	 cout << "Car Year: " << object.setYear() << end;
	 cout << "Car Make: " << object.setMake() << endl;
	 cout << "Car Model: " << object.setModel() << endl;
	 cout << "Car Speed: " << object.setSpeed() << endl;


	 cout << "Accelerating!!!!" << endl;
	 object.accelerate();
     cout << "The car's current speed is: " << object.getSpeed() << endl;
	 
	 cout << "Accelerating!!!!" << endl;
	 object.accelerate();
	 cout << "The car's current speed is: " << object.getSpeed() << endl;
	 
	 cout << "Accelerating!!!!" << endl;
	 object.accelerate();
	 cout << "The car's current speed is: " << object.getSpeed() << endl;

	 cout << "Time to brake!!!!" << endl;
	 object.brake();
	 cout << "The car's current speed is: " << object.getSpeed() << endl;

	 cout << "Time to brake!!!!" << endl;
	 object.brake();
	 cout << "The car's current speed is: " << object.getSpeed() << endl;

	 cout << "Time to brake!!!!" << endl;
	 object.brake();
	 cout << "The car's current speed is: " << object.getSpeed() << endl;

	 system("pause");
	 return 0;
}
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
#include <iostream>
#include <string>
#include <limits>
using namespace std;

class Car {
    int    year;
    string make; 
    string model;
    int    speed;
public:
    Car () : year(0), speed(0) {}
    Car(int year_, string model_, string make_, int speed_) {
        setYear(year_);
        setMake(make_);
        setModel(model_);
        setSpeed(speed_);
    }
    void setYear(int year_) {
        year = year_ > 1850 ? year_ : 1850;
    }
    void setModel(string model_) {
        model = model_;
    }
    void setMake(string make_) {
        make = make_;
    }
    void setSpeed(int s) {
        speed = s >= 0 ? s : 0;
    }
    void accelerate() {
        speed += 10;
    }
    void brake() {
        speed -= 10;
    }
    int    getYear()  { return year;  }
    string getMake()  { return make;  }
    string getModel() { return model; }
    int    getSpeed() { return speed; }
};

inline void ignoreline() {
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

void pause() {
    cout << "Paused (hit Enter): " << flush;
    ignoreline(); // wait for newline
}

int main() {
    int year, speed;
    string make, model;

    cout << "Car Year: ";    cin >> year;
    cout << "Car Make: ";    cin >> make;
    cout << "Car Model: ";   cin >> model;
    cout << "Car Speed: ";   cin >> speed;
    ignoreline(); // eat newline left in stream by cin>>

    Car object(year, make, model, speed); // use constructor to set fields

    cout << "Car Year: "  << object.getYear()  << endl;
    cout << "Car Make: "  << object.getMake()  << endl;
    cout << "Car Model: " << object.getModel() << endl;
    cout << "Car Speed: " << object.getSpeed() << endl;

    cout << "Accelerating!!!!\n";
    for (int i = 0; i < 3; i++) {
        object.accelerate();
        cout << "The car's current speed is: " << object.getSpeed() << endl;
    }

    cout << "Time to brake!!!!\n";
    for (int i = 0; i < 3; i++) {
        object.brake();
        cout << "The car's current speed is: " << object.getSpeed() << endl;
    }

    pause();
}


EDIT: I agree with SimpleCoder about not using system("pause"). And I couldn't see how the code as posted would have compiled, either!
Last edited on
I'm surprised it compiles since there are no overloaded setter functions that take no parameters but you call them in lines 136-139. The functions you do have, have no defaults, either, so they aren't called.

Check very carefully what functions you are calling when you try to output the car data to the console.

Since you have an overloaded constructor that accepts parameters, instead of calling each setter you could create the Car object:

Object car(year, model, make, speed);

You probably shouldn't use system("PAUSE") - it's not generally seen as safe. Ask for another input from the user to wait.

Topic archived. No new replies allowed.