I'm getting this error. I think it has something to do with how I'm calling the member functions brake and accelerate. http://prntscr.com/bzvjho
I have to design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method I have to get the current speed of the car and display it. Then I have to call the brake method five times and after each call the the brake method get the current speed of the car and display it.
I'm trying to get an output of:
Enter the car model's year: 1957
Enter the car's make: Chevy
The model year is 1957
The make is Chevy
The speed is 0
Let's see what it can do!!
The speed is,...
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
STOP! STOP! Let me OUT!
The speed is,...
145 140 135 130 125 120 115 110 105 100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0
#include <iostream>
#include "Car header.h"
usingnamespace std;
constint NUM_FOR_INCREASE=150;
constint NUM_FOR_DECREASE=0;
constint NUM_FOR_CAL=5;
int main()
{
int yearModel;
string make;
int speed=0;
cout << "Enter the car model year" << endl;
cin >> yearModel;
cout << "Enter the cars make" << endl;
cin >> make;
cout << "The model year is " << yearModel << endl;
cout << "The make is " << make << endl;
cout << "The speed is " << speed << endl;
cout << "Let's see what it can do." << endl;
cout << "The speed is,..." << endl;
Car carObject();
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
speed=speed+NUM_FOR_CAL
cout << speed << endl;//I'm not sure how to display the speed each time horizontally like I want in the output example
cout << "Stop! Stop! Let me out." << endl;
}
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
//150 now
speed=speed-NUM_FOR_CAL
cout << speed << endl; //I'm not sure how to display the speed each time horizontally like I want in the output example
cout << "I'll walk from here." << endl;
}
}
Then, put cout << "Stop! Stop! Let me out." << endl; in line 35 (after the loop) and cout << "I'll walk from here." << endl; in line 45 (after the loop, too).
Then, line 6 should be constint NUM_FOR_INCREASE=80; to get speed ranging from 5 to 150. With constint NUM_FOR_INCREASE=150; you'd get the speed ranging from 5 to 500!
So, if you only want to declare an object called carObject it should be Car carObject;
If you put Car carObject(); you are declaring the function carObject().
carObject.accelerate() and carObject.brake() are useless (I think, since I don't have the entire code). You already did the acceleration loop and brake loop inside main()
It would be a good idea to do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Car
{
public:
// Line 27 to 34 would be the accelerate() function in here
// Line 36 to 44 would be the brake() function in here
}
int main()
{
// Code till line 23 goes here
Car carObject;
carObject.accelerate();
carObject.brake();
}
Well, I tried that, but it actually gave me more errors than I had before haha. http://prntscr.com/bzxhzx
Well, if you read my OP, I need to call the accelerate and brake method from my header file into my main to use it. Did you see how I included a header in the top? That header has my class.
I'm trying to use the methods in them for my main program.
This is the full code (not including my header since I had no problem including it in my main program).
#include <iostream>
#include "Car header.h"
usingnamespace std;
constint NUM_FOR_INCREASE=80;
constint NUM_FOR_DECREASE=0;
constint NUM_FOR_CAL=5;
int main()
{
int yearModel;
string make;
int speed=0;
cout << "Enter the car model year" << endl;
cin >> yearModel;
cout << "Enter the cars make" << endl;
cin >> make;
cout << "The model year is " << yearModel << endl;
cout << "The make is " << make << endl;
cout << "The speed is " << speed << endl;
cout << "Let's see what it can do." << endl;
cout << "The speed is,..." << endl;
Car carObject;
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
speed=speed+NUM_FOR_CAL;
cout << speed << " ";//I'm not sure how to display the speed each time horizontally like I want in the output example
}
cout << "Stop! Stop! Let me out." << endl;
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
//150 now
speed=speed-NUM_FOR_CAL;
cout << speed << " "; //I'm not sure how to display the speed each time horizontally like I want in the output example
}
cout << "I'll walk from here." << endl;
}
Well, for the 2 last errors, just add a semi-collon at the end of line 29 and 38.
But the other error... Hmm... I don't really know what's going on =S
It's displayed with the last code you posted?
#include <iostream>
#include "Car header.h"
usingnamespace std;
constint NUM_FOR_INCREASE=80;
constint NUM_FOR_DECREASE=0;
constint NUM_FOR_CAL=5;
int main()
{
int yearModel;
string make;
int speed=0;
cout << "Enter the car model year" << endl;
cin >> yearModel;
cout << "Enter the cars make" << endl;
cin >> make;
cout << "The model year is " << yearModel << endl;
cout << "The make is " << make << endl;
cout << "The speed is " << speed << endl;
cout << "Let's see what it can do." << endl;
cout << "The speed is,..." << endl;
Car carObject;
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.accelerate()//My hope is this will add 5 to my speed variable here each time it's called
speed=speed+NUM_FOR_CAL;
cout << speed << " ";//I'm not sure how to display the speed each time horizontally like I want in the output example
}
cout << "Stop! Stop! Let me out." << endl;
for(int n=50; n<NUM_FOR_INCREASE; ++n)
{
carObject.brake()//My hope is this will decrease 5 to my speed variable here each time it's called (speed should be at
//150 now
speed=speed-NUM_FOR_CAL;
cout << speed << " "; //I'm not sure how to display the speed each time horizontally like I want in the output example
}
cout << "I'll walk from here." << endl;
}
What I'm trying to do is use the accelerate and brake function from the header in my main to do this:
I have to design a program that creates a Car object and then calls the accelerate method five times. After each call to the accelerate method I have to get the current speed of the car and display it. Then I have to call the brake method five times and after each call the the brake method get the current speed of the car and display it.
Well, you have a constructor Car(int ym, string mk) with 2 arguments and in line 25 in main() you wanna declare an object, but it's missing the arguments.
Because when you created the constructor Car, you assumed it would carry 2 parameters --> an int and a string.
If you want to declare an object with that constructor (Car with parameters), you HAVE to put the 2 parameters when declaring an object in main().
But I agree, this is a little bit strange... Another way to do it, is to create a constructor Car with no arguments.
To put in separate lines just use \n before the message in cout.
To start in 150, just alter the code to this:
1 2 3 4 5 6 7 8 9
for(int n = 50; n < NUM_FOR_INCREASE + 1; ++n)
{
carObject.brake();
cout << speed << " ";
speed = speed - NUM_FOR_CAL;
}
cout << "I'll walk from here." << endl;