2

I am new to c++ and trying to create a car class program that asks a user for a year and make of car. Then the program takes the speed, which always starts at 0 and accelerates 5mph 5 times and brakes 5 mph 5 times. I have to create the program with a header file and 2 cpp files. The return value for the speed is incorrect and comes up as:

Enter the year of the car: 2000 Enter the make of the car: Chevrolet The starting speed is -858993460

The current speed is: -858993455 mph.

The current speed is: -858993450 mph.

The current speed is: -858993445 mph.

The current speed is: -858993440 mph.

The current speed is: -858993435 mph.

The current speed is: -858993440 mph.

The current speed is: -858993445 mph.

The current speed is: -858993450 mph.

The current speed is: -858993455 mph.

The current speed is: -858993460 mph.

Press any key to continue . . .

Can anyone help me to figure out what I am doing wrong? I have attached what I have so far. Any help is greatly appreciated. Thanks

#define CAR_H
#include <string>
using namespace std;

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

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

};

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

Car::Car(int carYearModel, string carMake)
{
    int yearModel = carYearModel;
    string make = carMake;
int speed = 0;
}

void Car::accelerate()
{
    speed += 5;
}

void Car::brake()
{
    speed -= 5;
}

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

int getYear(int year)
{
    return year;
}

string getMake(string make)
{
return make;
}

#include "Car.h"
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

int main()
{
    int count;
int yr;
string mk;
int getSpeed;

cout << "Enter the year of the car: ";
cin >> yr;

cout << "Enter the make of the car: ";
cin >> mk;

Car myCar(yr, mk);

    cout << "The starting speed is "  
    <<  myCar.getSpeed() << endl << endl;

    for ( count = 0; count < 5; count++)
    {
        myCar.accelerate();
        cout << "The current speed is: " << myCar.getSpeed() 
        << " mph." << endl;
    } 

    for ( count = 0; count < 5; count++)
    {
        myCar.brake();
        cout << "The current speed is: " << myCar.getSpeed() 
        << " mph." << endl;
    }

    system ("pause");

    return 0;
}
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Chevygal1969
  • 49
  • 3
  • 7
  • Because you declare a variable in your constructor with the same name as a member variable. `int speed = 0;` should be `speed = 0;` - or better yet initialize the in the constructor initializer list. – Captain Obvlious Apr 25 '14 at 20:01
  • 1
    0xcccccccc == -858993460 == using an uninitialized variable. Remove *int*. – Hans Passant Apr 25 '14 at 20:04
  • 1
    -858993460 = 0xCCCCCCCC which means [you've accessed uninitialized memory](https://stackoverflow.com/q/370195/995714) – phuclv Aug 18 '18 at 11:06

1 Answers1

7

In this code:

 Car::Car(int carYearModel, string carMake)
 {
      int yearModel = carYearModel;
      string make = carMake;
      int speed = 0;
 }

You are not assigning to the data members of the Car object. Instead, you're declaring local variables with the same names as the fields, then assigning to those local variables.

To fix this, remove the types:

 Car::Car(int carYearModel, string carMake)
 {
      yearModel = carYearModel;
      make = carMake;
      speed = 0;
 }

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 1
    Should probably use initializer lists instead. (as in, `Car::Car(int carYearModel, string carMake) : yearModel(carYearModel), make(carMake), speed(0) {}`) – fstd Apr 25 '14 at 20:05
  • @fstd Yes, that's an excellent point. My hope with this answer was to show how the types were changing the meaning of the code, but you're correct that the best way to do this is with initializer lists. – templatetypedef Apr 25 '14 at 20:26
  • Thank you so much templatetypedef! That helped and did the trick! – Chevygal1969 Apr 25 '14 at 21:29