quick problem with class member variable not being saved

within my code i'm supposed to create a code where i get the speed of a car and add 5 to that speed with each loop and then display the value after each loop finishes, so far it only displays the number i originally put it. i think i messed up somewhere with the accessors and mutators


Car.h file
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
#ifndef CAR_H
#define CAR_H
#include <string>
using namespace std;

class Car
{
private:
	int carYear;
	double carSpeed;
	string carMake;
public:
	/*constructor*/
	Car(int, string);			
	Car();				//default constructor


	/*accessors*/
	void setValues(double, int, string);		   

	/*mutators*/
	double getSpeed();
	int getYear();
	string getMake();		


	double accelerateFunction();
	double brakeFunction();
	
	/*deconstructor*/
	~Car();
		
};
#endif 



car.cpp
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
//this is the class function implementation file
#include "Car.h"
#include <string>
#include <iostream>
using namespace std;

/*constuctor function*/
Car::Car(int localCarYear, string localCarMake)		
{
	carYear = localCarYear;
	carMake = localCarMake;
	carSpeed = 0;

}
Car::Car()
{
	carYear = 0;
	carMake = "/n";
}
/*end of constructor*/



/*accessor functions*/
void Car::setValues(double someSpeed, int someYear, string theMake)
{
	carSpeed = someSpeed;
	carYear = someYear;
	carMake = theMake;
}
/*end of accessors*/




/*mmutator functions*/
double Car::getSpeed()
{
	return carSpeed;	
}

int Car::getYear()
{
	return carYear;
}
string Car::getMake()
{
	return carMake;
}
/*end of mutators*/



double Car::accelerateFunction()
{
	carSpeed = carSpeed + 5;
	return carSpeed;
}

double Car::brakeFunction()
{
	return carSpeed = -5;
}

/*deconstructor function*/
Car::~Car()
{
	cout << endl << "Thank you for safe driving!" << endl;
}



main.cpp
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
#include "Car.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	/*declarations*/
	Car obj;
	int year;
	double speed;
	string make;
	bool test = true;

	do {
		cout << "please enter the speed of the car: ";
		cin >> speed;

		if (speed < 0)
		{
			cout << "invalid number, please type in a numbe above 0" << endl << endl;
			test = false;
		}
		else if (speed > 0)
		{
			test = true;
		}
	} while (test == false);



	do {
		cout << "please enter the year of the carr (yyyy): ";
		cin >> year;
		if (year < 1886)
		{
			cout << "please type in a year past 1886" << endl << endl;
			test = false;
		}
		else if (year > 1886)
		{
			test = true;
		}
	} while (test == false);

	cout << "please enter the make name of the car: ";
	getline(cin, make);
	cin.ignore();

	obj.setValues(speed, year, make);

	for (int i = 0; i < 5; i++)
	{
		obj.accelerateFunction();
		cout << "the current speed is: " << speed << endl;
		
	}

	system("pause");
	return 0;
}

Last edited on
Hi,
> I'm supposed to create a problem.
So you want to make your life bitter and boring? Are you trying to challenge us?
oh whoops lmao, fixed now
Last edited on
Hi,
This :
cout << "The current speed is: " << speed << endl;

Should be :
cout << "The current speed is: " << obj.getSpeed() << endl;
(Does that help you?) :)
if i type that in, i get an error that says
'Car::getSpeed': non-standard syntax; use '&' to create a pointer to member

edit: nvm, forgot parenthesis, thank you so much!
Last edited on
> How do i make the destructor's message show at the end of the main.cpp?

1
2
// In Car() class
~Car() {cout << "Car has been destroyed!" << endl;}
closed account (E0p9LyTq)
how do i make the destructor's message show at the end of the main.cpp?


Open up a command prompt console window and run your app from that window. When your app exits the window won't close.

If you are running Windows 10 you can easily start a command prompt window in Windows File Explorer. Navigate to the directory where your program's exe file is located and in File Explorer's address bar type "cmd" and hit enter.

The command prompt window automatically has the location set so all you have to do is type the name of your program file.
i figured out that the system("pause") was stopping my destructor from showing up. i know there's some talk about not using that line of code unless you're using it for homework or etc. what's a work around for that? for now i just added a breakpoint at the curly brace of end of int main() brace and i saw my destructor message
closed account (E0p9LyTq)
Using any system command is "iffy." They are specific to the OS. What works for Windows may not work with Mac or *nix.

There are other "beginner" things that can end up causing difficulties later, such as "using namespace std;" Not, it is not wrong or bad, just not recommended.

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice

I personally don't use it, I prefer to use explicit declarations such as std::cout. :)

1
2
3
4
double Car::brakeFunction()
{
	return carSpeed = -5;
}
you are going to ruin your gearbox like that.


> i figured out that the system("pause") was stopping my destructor from showing up.
FurryGuy wrote:
Open up a command prompt console window and run your app from that window. When your app exits the window won't close.
Topic archived. No new replies allowed.