I have to make a class called Car with the member variables yearModel make and speed
My constructor has to set speed to 0 and accept the year model and make as arguments. These objects should be assigned to the object's yearModel and make member variables.
Appropriate accessor functions to get the values stored in an object's yearModel make and speed member variables.
I now have it running but getting bad output. My make comes up with a funny symbol and my speed shows up zero... if I comment out the speed = 0; part the speed shows up as a crazy negative number.
What PanGalactic is saying that your statement: make[size] = mk[size];
will assign the value stored in the 21th element of "mk" to the 21th element of variable "make". Which will most likely will be a NULL ('\0') or garbage.
Arrays are to be handled differently and most learners make this mistake. Use strcpy(make, mk); function (string.h header). Which copies the elements of "mk" to "make" one by one.
You could also use a loop like this:
1 2
int counter = 0;
while((make[counter] = mk[counter++]) != '\0') ;
An unassigned variable contains garbage so no wonder you got a negative number.
Also instead of doing speed = 0;
You should be doing speed = sp;
In the Car constructor (line: 24)
Apart from that I failed to see any other errors. So good luck on improving.
No, what I am saying is that a) that's no way to do string assignment; and b) make[size] = mk[size] will result in two out-of-bounds array accesses. Since speed is declared after the make array, that assignment is almost certainly affecting speed and certainly not affecting make.
@PanGalactic Wait... no way to do a string assignment?
I've been using Java for a long while now so my C++ isn't that sharp (great joke I heard), but I think its highly unlikely that there would be 'no way' to do anything in programming.
Example
1 2 3 4
int myFunction(string p) {
classString = p; // This should work fine
}
I think you misinterpreted my meaning. My sentence said "that's [a contraction of 'that is' where 'that' refers to 'make[size] = mk[size]'] no way to do string assignment."