Hello. What im tryin to do is answer the following.
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.


I have developed my code, And something is just not right, but im pretty close to getting it working. My other question is did i take it to far and answered the wrong question like should i let the user call the function or should i just automatically call accelerate and brake i thought about it to much that i confused my self. My code will be right after this message.

Recommended Answers

All 7 Replies

Ok here is what i have for the code.

#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. ";
				cout << Accelerate(first) << endl;
				break;
			case 'b':
			case 'B': cout << "You have choosen to push the brake.";
				cout << Brake(first) << endl;
				break;
			}
		}while (toupper(choice) != 'C');

	return 0;
}

So let me know if i answered the question and how to fix these errors found in the last section i think it was line 90 and 94.
: error C3861: 'Accelerate': identifier not found
: error C3861: 'Brake': identifier not found
: fatal error C1075: end of file found before the left brace '{' at 'c:\users\

class.function()

So it's:

first.Accelerate();

It alls here :

{
			switch (choice)
			{
			case 'a':
			case 'A': cout << "You are accelerating the car. ";
				cout << Accelerate(first) << endl;
				break;
			case 'b':
			case 'B': cout << "You have choosen to push the brake.";
				cout << Brake(first) << endl;
				break;
			}
		}while (toupper(choice) != 'C');

You need to use the member to call the function Accelerate,and
brake, like so first.Accelerate(...), first.brake(...), and also you
are missing a bracket.

//Process the user's menu selection
		{ <--//either remove this or
}   
                    } //<-- or add this.
		}while (toupper(choice) != 'C');

Ok, let me try the advice to fix the errors. and that bracket i thought it was something more serious. I totally didn't catch that.

As far as the original question in my first post, does this program answer that, or should i modify it so that it runs by itself without the user selecting a choice.

Well, doing a little extra work on an assignment is almost always a good thing. On the other hand the assignment specifies:

Demonstrate the class in a program that creates a Car object and then calls the accelerate function five times.

You're solution doesn't do this so you could possibly lose marks. Make sure you read the requirements carefully, ensure that they are all met, and then add something extra like the menu driven options.

Thanks that helps, I should of asked first before doing the program, but its ok, this was fun.

You're program is fine. Just run a loop to run accelerate 5 times before showing your menu.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.