this is for the Car class assignment for C++. However when i try to execute it a
ID: 3596102 • Letter: T
Question
this is for the Car class assignment for C++. However when i try to execute it and debug it , it keeps saying this
Severity Code Description Project File Line Suppression State
Error LNK1561 entry point must be defined Project18 LINK at line 1
Here is my code below, what am i doing wrong?
#include<iostream>
#include<string>
using namespace std;
//Car class declaration
class Car
{
private:
int yearMade, speed;
string make;
public:
Car(int y, string m)
yearMade = y;
make = m;
speed = 0;
int getYear();
int getSpeed();
string getMake();
//Car class member functions and accesors
int getYear()
{
return yearMade;
}
string getMake()
{
return make;
}
int getSpeed()
{
return speed;
}
void accelerate()
{
speed = speed + 5;
}
void brake()
{
speed = speed - 5;
}
};
int main()
{
Car carObj;
//Call the accelerate function five times and display current speed;
for (int faster; faster >= 5; faster++)
{
carObj.accelerate();
cout << "The current speed when accelerating is: " << carObj.getSpeed() << endl;
}
for (int slower; slower >= 5; slower++)
{
carObj.brake();
cout << "Now the current speed is: " << carObj.getSpeed();
}
cout << endl;
system("pause");
return 0;
}
Explanation / Answer
Note: I have updated the code
#include<iostream>
#include<string>
using namespace std;
//Car class declaration
class Car
{
private:
int yearMade, speed;
string make;
public:
Car(int y, string m)
{
yearMade = y;
make = m;
speed = 0;
}
//Car class member functions and accesors
int getYear()
{
return yearMade;
}
string getMake()
{
return make;
}
int getSpeed()
{
return speed;
}
void accelerate()
{
speed = speed + 5;
}
void brake()
{
speed = speed - 5;
}
};
int main()
{
Car carObj(2011,"abc");
//Call the accelerate function five times and display current speed;
for (int faster=0; faster < 5; faster++)
{
carObj.accelerate();
cout << "The current speed when accelerating is: " << carObj.getSpeed() << endl;
}
for (int slower=0; slower < 5; slower++)
{
carObj.brake();
cout << "Now the current speed is: " << carObj.getSpeed();
}
cout << endl;
return 0;
}
----------------------------
problem with the code:
1. You did not pass the values to the constructor
2.problem is in the for loop to accelerate the speed for five times
for (int faster; faster >= 5; faster++)
you have not intialized the faster variable .
so faster will take garbaze value .so you need to intialize to zero
to accelerate five times the condition would be like faster<5
3.
.problem is in the for loop to slower the speed for five times
for (int slower; slower >= 5; slower++)
you have not intialized the slower variable .
so slower will take garbaze value .so you need to intialize to zero
to decrease speed f five times the condition would be like slower<5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.