Question

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;
}
Était-ce utile?

La solution

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!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top