#include <iostream>
#include<string.h>
using namespace std;
class Car{
public:
void opengate(){
strcpy(gate,"ON");
}
void closegate(){
strcpy(gate,"OFF");
}
void openlight(){
strcpy(bull,"ON");
}
void closelight(){
strcpy(bull,"OFF");
}
void plus(){
speed+=10;
}
void div(){
speed-=10;
}
void printfInfo(){
cout<<"车门"<<" "<<gate<<endl;
cout<<"车灯"<<" "<<bull<<endl;
cout<<"速度"<<" "<<speed<<endl;
}
char gate[20];
char bull[20];
int speed;
};
int main()
{
Car car;
strcpy(car.gate,"OFF");
strcpy(car.bull,"OFF");
car.speed=0;
char a[20];
cin>>a;
for(int i=0;i<20;i++){
if(a[i]==1){
car.opengate();
}
if(a[i]==2){
car.closegate();
}
if(a[i]==3){
car.openlight();
}
if(a[i]==4){
car.closelight();
}
if(a[i]==5){
car.plus();
}
if(a[i]==6){
car.div();
}
else
break;
}
car.printfInfo();
return 0;
}
一个else只能对应一个if,所以除了最后一个else和第一个if,其他的都改成else if
不知道你问编译错还是运行结果错
char a[20];
cin>>a;
for(int i=0;i<20;i++)
不能确定输入的a串有20个字符,a串没有初始化,如果不足20个,剩余字符可能是任何字符,导致结果不正确
i<20 你确定你输入的是20个吗