Class function not updating variable

I am looking for a hint to where my program is failing. The program uses a default constructor to set the values null and an overloaded constructor to initialize the variable names and set speed to "0". from there I need to use the setSpeed function in my Accelerate and Brake functions to set the increased or decreased speed. As of now the program compiles but when I select the option to accelerate or brake my speed remains at 0. Im guessing my functions are not updating properly
so when I getSpeed its just showing the default value.

here is my Header:
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
#ifndef CAR_H
#define CAR_H
#include <string>

using namespace std;

class Car
{
    private:
        int year;
        int speed;
        string make;


    public:
        void setYear(int);
        void setMake(string);
        void setSpeed(int);
        int getSpeed();
        int accelerate(int);
        int brake(int);
        Car();
        Car(int, int, string);


};

#endif // CAR_H 

and my .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
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
#include "Car.hpp"
#include <iostream>
#include <string>

    using namespace std;

    int main()
{
Car mine;

    int yearMade, speed, counter,choice;
    string brand;

    cout << "\nThis program will ask you to for the year, make and speed ";
    cout << "\n of the car you want. Then it will call the accelerate function ";
    cout << "\n9 times followed by the brake function nine time and printout the ";
    cout << "\nspeed as it goes." << endl;

    cout << "\n\nEnter the year your car was made: ";
        cin  >> yearMade;
    cout << "\nEnter the make of your car: ";
        cin >> brand;

        mine.setYear(yearMade);
        mine.setMake(brand);


        do//a 'do' 'while' loop for the function of the menu
        {
            cout << "\nPlease Select an Option to accelerate or brake\n\n";
            cout << "1. Accelerates by 10.\n";
            cout << "2. Decelerates by 10\n";
            cout << "3. Quit.\n";
            cout << "\nEnter a choice: ";
                cin >> choice;

          if (choice==1)

            {
                mine.accelerate(speed);

                cout << "\nYour acceleration is: " << mine.getSpeed() << endl;
            }
                else if (choice==2)

                        {
                            mine.brake(speed);

                         cout << "\nYour car deceleration is: " << mine.getSpeed() << endl;
                         }

                         else if (choice==3)
                         {
                             cout <<"\nQuiting...." << endl;

                             return 0;
                         }
        }
                         while ((choice > 0) && (choice < 4));


    return 0;
 }
              /*****************************************************
              *                    Car::Car()                      *
              * The default constructor, used to set null values.  *
              * in function year is set to 0, make is "", and speed*
              *is 0.                                              *
              *****************************************************/
                        Car::Car()
                        {
                        year = 0;
                        speed = 0;
                        make = "";
                        }

              /*****************************************************
               *             Car::Car(int, int, string)             *
               * The overload constructor, used to initialize       *
               * variables. In this function year is y and make is  *
               * mk, and speed is set to 0.                         *
               *****************************************************/
                        Car::Car(int yearMade, int sp, string brand)
                        {
                        setYear(yearMade);
                        setSpeed(0);
                        setMake(brand);

                        }
               /*****************************************************
                *             Car::setYear(int)                      *
                * This function receives the user input from main    *
                * and sets the year variable to that value.          *
                *****************************************************/
                        void Car::setYear(int yearMade)
                        {
                        year = yearMade;
                        }
               /*****************************************************
                *             Car::setMake(string)                   *
                * This function receives the user input from main    *
                * and sets the make variable to a string.            *
                *****************************************************/
                        void Car::setMake(string brand)
                        {
                        make = brand;
                        }
               /*****************************************************
                *             Car::setSpeed(int)                     *
                * This function receives input from the accelerate   *
                * and brake functions to add or subtract 10 speed.   *
                *****************************************************/
                        void Car::setSpeed(int)
                        {
                           speed = speed;
                        }
                /*****************************************************
                 *             Car::getSpeed(int)                     *
                 * This function retrieves speed from the setSpeed    *
                 * function to output the speed in main.              *
                 ***************************************************/
                        int Car::getSpeed()
                        {
                            return speed;
                        }
                 /*****************************************************
                  *             Car::accelerate(int)                   *
                  * When this function is called it will add 10 to the *
                  * current speed and set the new value.               *
                  *****************************************************/
                        int Car::accelerate(int speed)
                        {
                            if (speed < 80)
                            {
                                setSpeed(speed + 10);
                                return 0;
                            }
                        }
                 /*****************************************************
                  *                Car::brake(int)                     *
                  * When this function is called it will deduct 10 from*
                  * the current speed and set the new value.            *
                  *****************************************************/
                        int Car::brake(int speed)
                        {
                            if(speed >= 0)
                                {
                                    setSpeed(speed - 10);
                                    return 0;
                                }
                        }

Do you see any problem with line 115?
I do, and I have been really confused on what that should be since it uses a default value initially and not an input. I am sure the answer is obvious and I am am over thinking it a bit.

So, setSpeed should be changed from its initial value by the accelerate and brake functions, but I am not sure how to set the argument for that in setSpeed. Would i use an if else statement and have speed = brake(speed) or accelerate(speed)?
Something important is missing on line 113. Hint: compare it to line 131.
Last edited on
LB,

Would that be the variable input, speed?
Your accelerate() method takes a parameter called speed. But the class also has a member called speed. So if you use "speed" inside the method, which one do you mean? The class member? The parameter? This is like throwing marbles on the floor: it's guaranteed to trip you up. By the way, the compile will use the parameter, not the class member.

For this reason, it's a good idea to never use a class member name as a parameter of the class's method. Let's change the parameter to in_speed:
1
2
3
4
5
6
7
8
int Car::accelerate(int in_speed)
{
    if (in_speed < 80)
    {
         setSpeed(in_speed + 10);
         return 0;
    }
}


The same logic applies to brake(). You
Hmm... This doesn't make sense does it? The comments say that it should just increase your speed by 10. It looks like your code meant the member variable "speed" instead of the parameter. Let's fix that:
1
2
3
4
5
6
7
8
int Car::accelerate(int in_speed)
{
    if (speed < 80)
    {
         setSpeed(speed + 10);
         return 0;
    }
}

Notice that the parameter in_speed is never used now. So why do you need it?

The same problem is in brake(). The code is using the parameter speed, not the member variable.

Now let's look at setSpeed:
1
2
3
4
void Car::setSpeed(int)
{
      speed = speed;
}

You haven't named the parameter, which means it won't be used. That should make a big loud bell go off in your head. How can you set the speed when you haven't even named the parameter that contains the value you're setting it to??

The statement speed = speed; takes the member variable speed and assigns it to itself. That's a big "do nothing" statement and the compiler will optimize it out of your code. You should be able to fix this based on the other comments above on accelerate and brake
Thank you for all the replies its been a big help. Ill work through the suggestions and post my results.
So here were the corrections I made, I think I followed the advise but I did change my functions from int to void. It compiles and runs correctly now.



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
  
/*****************************************************
                *             Car::setSpeed(int)                     *
                * This function receives input from the accelerate   *
                * and brake functions to add or subtract 10 speed.   *
                *****************************************************/
                        void Car::setSpeed(int in_speed)
                        {
                           speed = in_speed;
                        }
                /*****************************************************
                 *             Car::getSpeed(int)                     *
                 * This function retrieves speed from the setSpeed    *
                 * function to output the speed in main.              *
                 ***************************************************/
                        int Car::getSpeed()
                        {
                            return speed;
                        }
                 /*****************************************************
                  *             Car::accelerate(int)                   *
                  * When this function is called it will add 10 to the *
                  * current speed and set the new value.               *
                  *****************************************************/
                        void Car::accelerate()
                        {
                            if (speed < 80)
                            {
                                setSpeed(speed = speed + 10);

                            }
                        }
                 /*****************************************************
                  *                Car::brake(int)                     *
                  * When this function is called it will deduct 10 from*
                  * the current speed and set the new value.            *
                  *****************************************************/
                        void Car::brake()
                        {
                            if(speed > 0)
                                {
                                    setSpeed(speed - 10);
                                }

                        }
Topic archived. No new replies allowed.