嘿傢伙,所以我被分配到調試和修復給定的代碼。在我們修復它之後,這個任務應該是這樣工作的: 在創建Car對象的程序中演示該類,然後調用五次加速函數 。每次調用加速功能後,獲取當前的車速並顯示它。然後,調用制動功能六次。在每次撥打剎車功能後,獲取當前的車速並顯示它爲什麼不在制動範圍內宣佈制動和加速?
這就是我所擁有的 - 問題是一旦運行我得到一個錯誤,說「加速」和「剎車未聲明在範圍內這個範圍「這很奇怪,因爲它們的功能應該放在正確的位置。讓我知道如果我錯過了什麼謝謝!
#include <math.h>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
class Car
{
private:
int YearModel;
int Speed;
string Make;
public:
Car(int, string, int);
string getMake();
int getModel();
int getSpeed();
int Accelerate(int aSpd);
int Brake(int bSpd);
void displayMenu();
};
Car::Car(int YearofModel, string Makeby, int Spd)
{
YearModel = YearofModel;
Make = Makeby;
Speed = Spd;
}
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.
int Car::Accelerate(int aSpd)
{
aSpd = Speed;
Speed = Speed + 5;
return aSpd;
}
//To drop the speed of the car by 5.
int Car::Brake(int bSpd)
{
bSpd = Speed;
Speed = Speed - 5;
return bSpd;
}
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
int year;
string carModel;
cout << "Enter car year: ";
cin >> year;
cout << "Enter the car model(without spaces): ";
cin >> carModel;
Car first(year, carModel, 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;
system("pause");
}
那好吧甜,修復了。但是現在當我運行程序時,它只是讓我輸入模型並製作(這很奇怪),然後顯示3和0,然後按任意鍵然後完成...不應該顯示菜單和無論我想加速還是休息? –
我說它很奇怪,因爲在int main中我有它,所以我把輸入車年然後模型。不是在同一時間... –
所以應該這樣寫?汽車第一=新車(年份,車型,速度); 如果是這樣說它從車*轉換到非標量型車請求錯誤\ –