我需要在课堂上使用数组存储所有汽车信息并打印出来。 任何人都可以帮我编辑T.T. 他们在登记他们的汽车之后使用开关,他们想要注册多少辆汽车。它需要返回菜单并引导他们进行第二选择以更新其信息。 第三是打印出他们想要打印的那个,他们可以键入而不打印全部 第四,但是第四个是打印出所有。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Vehicle
{
protected:
string Manufacturer;
string Type;
int Seatnumber;
public:
Vehicle()
{
Manufacturer="";
Type="";
Seatnumber=1;
};
Vehicle(string manufacturer,string type,int seatnumber)
{ Manufacturer=manufacturer;
Type=type;
Seatnumber=seatnumber;
};
void setmanufacturer(string manufacturer)
{ Manufacturer=manufacturer;
};
void settype(string type)
{ Type=type;
};
void setseatnumber(int seatnumber)
{ Seatnumber=seatnumber;
};
string getmanufacturer()
{ return Manufacturer;
}
string gettype()
{ return Type;
};
int getseatnumber()
{ return Seatnumber;
};
};
class Car: public Vehicle
{
protected:
string Carplatenumber;
string Model;
public:
Car():Vehicle()
{
Carplatenumber="";
Model="";
};
Car(string carplatenumber, string model)
{ Carplatenumber=carplatenumber;
Model=model;
};
void setcarplatenumber(string carplatenumber)
{ Carplatenumber=carplatenumber;
};
void setmodel(string model)
{ Model=model;
};
string getcarplatenumber()
{ return Carplatenumber;
};
string getmodel()
{ return Model;
};
void display();
};
inline void Car::display(){
cout<<Manufacturer;
cout<<Type;
cout<<Seatnumber;
cout<<Carplatenumber;
cout<<Model;
};
int main ()
{
char choice;
int size=0,seats;
Car car[size];
string manufacturer,type,carplatenumber,model;
cout<<"Please choice a selection: ";
cin>>choice;
bool True=true;
while(True){
switch(choice){
case 'A':
True=false;
cout<<"How many cars information do you want to store in it : ";
cin>>size;
cout<<"\n";
for(int i=0;i<size;i++){
cout << "Enter car "<<i+1<<" Manufacturer : ";
getline (cin,manufacturer);
cin.ignore();
car[size].setmanufacturer(manufacturer);
cout << "Enter car "<<i+1<<" Type : ";
getline (cin,type);
cin.ignore();
car[size].settype(type);
cout << "Enter car "<<i+1<<" Seatsnumber of the car : ";
cin >> seats;
cin.ignore();
car[size].setseatnumber(seats);
cout << "Enter car "<<i+1<<" Model : ";
getline (cin,model);
cin.ignore();
car[size].setmodel(model);
True=true;
}
break;
case'B':
True=false;
cout << "Hi,there please update your carplatenumber after u have gone through your new car!";
getline(cin,carplatenumber);
car[size].setcarplatenumber(carplatenumber);
True=true;
break;
case'c':
True=false;
for(int i=0;i<size;i++){
car[i].display();
}
True=true;
break;
default:cout<<"Invalid input";
}
}
return 0;
}
答案 0 :(得分:1)
首先,我建议您准确说出您遇到的问题。对于想要帮助你的人来说,这是非常重要的一点。
其次,C ++是区分大小写的编程语言,'A'和'a'是不同的。当然是'c'和'C'。
第三,关于你的问题。你错过了一次手术。cin>>choice;
bool True=true;
while(True){
switch(choice){
case 'A':
// some action
break;
case'B':
// some action
break;
case'c':
// some action
break;
default:
// some action
}
// this is the last line of loop
}
在最后一行循环之后,选择的价值是多少? True的价值是多少(实际上不是一个非常好的名字)?
缩进对于代码阅读器也非常重要。你可以阅读谷歌风格指南