The Assignment is:
Write a class named Car that has the following member variables:
yearModel, make, speed. In addition the class should have the following constructor and the other member functions
constructor-accept the cars year model make as arguments. an assign to the objects year, make also assign speed 0 to speed member.
Accessors to get the values stored in an object's yearModel make, speed
accelerate- should add 5 to the speed member variable each time it is called.
brake- subtract 5 from speed each time it is called.

Demonstrate the class in a program that creates a Car object and then calls the accelerate function five times. After each call to the accelerate function get the current speed of the car and display it. Then call the brake function five times after each call to the brake fuction, get the current speed. of the car and display it.

What else do i need to add to make the assignment correct, ive tried a few different things and cant get it to work.

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

class Car
{
private:
	int YearModel;
	int Speed;
	string Make;
public:
	Car(int, string, int);
	string getMake();
	int getModel();
	int getSpeed();
	void Accelerate();
	void Brake();
	void displayMenu();
};

Car::Car(int YearofModel, string Makeby, int Spd)
{
YearModel = YearofModel;
Make = Makeby;
Speed = Spd;
}
//To get who makes the car.
string Car::getMake()
{
	return Make;
}
//To get the year of the car.
int Car::getModel()
{
	return YearModel;
}
//To holds the car actual speed.
int Car::getSpeed()
{
	return Speed;
}
//To increase speed by 5.
void Car::Accelerate()
{
	Speed = Speed + 5;
}
//To drop the speed of the car by 5.
void Car::Brake()
{
	Speed = Speed - 5;
}

void displayMenu()
{
	cout <<"\n				Menu\n";
	cout << "----------------------------\n";
	cout << "A)Accelerate the Car\n";
	cout << "B)Push the Brake on the Car\n";
	cout << "C)Exit the program\n\n";
	cout << "Enter your choice: ";
}

int main()
{
	int Speed = 0; //Start Cars speed at zero.
	char choice; //Menu selection

	cout << "The speed of the SUV is set to: " << Speed <<endl;
	Car first( 2007, "GMC", Speed);

	//Display the menu and get a valid selection
	do
	{
		displayMenu();
		cin >> choice;
		while(toupper(choice) < 'A' || toupper(choice) > 'C')
		{
			cout << "Please make a choice of A or B or C:";
			cin >> choice;
		}

		//Process the user's menu selection
		{
{
			switch (choice)
			{
			case 'a':
			case 'A': cout << "You are accelerating the car. ";
				first.Accelerate();
				break;
			case 'b':
			case 'B': cout << "You have choosen to push the brake.";
				first.Brake();
				break;
			}

}
		}
		}while (toupper(choice) != 'C');

	return 0;
}

The assignment says "get the current speed of the car and display it". You do not!
So.. you should display the speed of the car when you accelerate and brake.

The assignment says "get the current speed of the car and display it". You do not!
So.. you should display the speed of the car when you accelerate and brake.

I cant get it to do that, ive tried and can figure it out. it always says speed 0

If you print out first.getSpeed() after you have called accelerate and brake it should work. Maybe you are using the local variable Speed (which is 0).

This article has been dead for over six months. Start a new discussion instead.