好的,我有一个问题让这个工作,所以有人请帮助我这个我试图要求用户搜索一个站名称,如果找到输出它,如果没有然后退出..
struct Trains
{
int Train_Id; //train ID
string Station_Origin; //Station of Origin
string Destination_Station; //Station of Destination
int Departure_Time ; //Departure Time
int Arrival_Time ; //Arrival Time
};
struct Trains myArray[10];
struct station
{
string intermediate_station1; //intermediate_station1
int Arrival_time1; // Arrival time to station1
string intermediate_station2; //intermediate_station2
int Arrival_time2; // Arrival time to station2
string intermediate_station3; // intermediate_station1
int Arrival_time3; // Arrival time to station3
};
struct station myArray1[3];
/*****************************************************************************/
int main ()
{
/* Setting up Train and Station Information */
int N_TRAIN;
int n;
int a;
do
{
cout << " Enter number of Trains on Track: " <<endl;
cin >> N_TRAIN;
cout << endl;
}while(N_TRAIN > 10 || N_TRAIN < 0);
for(n=0; n < N_TRAIN; n++)
{
while((cout<<" Enter Train ID: ")&&( (cin>>myArray[n].Train_Id)||myArray[n].Train_Id <0))
{
cout<<" Invalid Input! Please Enter Train ID: "<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
cout << " Name of Station of Origin: ";
cin >> myArray[n].Station_Origin;
cout << " Name of Destination Station: ";
cin >> myArray[n].Destination_Station;
while((cout<<"Enter Departure Time: Please Enter 24hr!")&&(!(cin>>myArray[n].Departure_Time)||myArray[n].Departure_Time < 0))
{
cout<<" Invalid Input! Enter Departure Time: Please Enter 24hr !"<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
while((cout<<" Enter Arrival Time: Please Enter 24hr! ")&&(!(cin>>myArray[n].Arrival_Time)||myArray[n].Arrival_Time < 0))
{
cout<<" Invalid Input! Enter Arrival Time: Please Enter 24hr! "<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
cout << " Name of Station of intermediate station1: ";
cin >> myArray1[a].intermediate_station1;
while((cout<<" Enter Arrival Time For intermediate station1: Please Enter 24hr! ")&&(!(cin>>myArray1[a].Arrival_time1)||myArray1[a].Arrival_time1 < 0))
{
cout<<" Invalid Input! Enter Arrival Time: Please Enter 24hr! "<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
cout << " Name of Station of intermediate station2: ";
cin >> myArray1[a].intermediate_station2;
while((cout<<" Enter Arrival Time For intermediate station2: Please Enter 24hr! ")&&(!(cin>>myArray1[a].Arrival_time2)||myArray1[a].Arrival_time2 < 0))
{
cout<<" Invalid Input! Enter Arrival Time For intermediate station2: Please Enter 24hr!"<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
cout << " Name of Station of intermediate station3: ";
cin >> myArray1[a].intermediate_station3;
while((cout<<" Enter Arrival Time For intermediate station3: Please Enter 24hr!")&&(!(cin>>myArray1[a].Arrival_time3)||myArray1[a].Arrival_time3 < 0))
{
cout<<" Invalid Input! Enter Arrival Time For intermediate station3: Please Enter 24hr!"<<endl;
cin.clear();
cin.ignore(1000,'\n');
}
}
/* SEARCHING BY STATION NAME: */
else if (choice == 3)
{
int origin;
int result;
for (n=0; n < N_TRAIN ;n++)
{
cout << " Enter the Name of Departure Station: " ;
cin >> origin;
origin = searchfs();
if (origin = myArray[n].Station_Origin )
cout << "Trains Departs " << " " <<myArray[n].Station_Origin << "at" << " " << myArray[n].Departure_Time << " " << " Hours " <<endl;
else if (origin = myArray[n].Destination_Station )
cout << "Trains Departs " << " " <<myArray[n].Destination_Station << "at" << " " << myArray[n].Departure_Time << " " << " Hours " <<endl;
else if (origin = myArray1[a].intermediate_station1 )
cout << "Trains Departs " << " " << myArray1[a].intermediate_station1<< "at" << " " << myArray1[a].Arrival_time1 << " " << " Hours " <<endl;
else if (origin = myArray1[a].intermediate_station2 )
cout << "Trains Departs " << " " << myArray1[a].intermediate_station2 << "at" << " " << myArray1[a].Arrival_time2 << " " << " Hours " <<endl;
else if (origin = myArray1[a].intermediate_station3 )
cout << "Trains Departs " << " " << myArray1[a].intermediate_station3<< "at" << " " << myArray1[a].Arrival_time3 << " " << " Hours " <<endl;
else if (result == -1)
cout << " Train doesnt not Depart at "<< " " << origin << endl;
}
}
答案 0 :(得分:2)
在一些地方看起来你有一个任务(单=
)你打算测试相等(双==
),如下例所示:
if (origin = myArray[n].Station_Origin)
此外,Station_Origin
和intermediate_station1/2/3
定义为std::string
,而origin
定义为int
。您正在尝试将string
分配或比较为int
。
答案 1 :(得分:0)
您还没有告诉我们 正在发生什么,所以我们只能通过扫描代码来猜测。令我印象深刻的是:
for(n=0; n < N_TRAIN; n++)
{
...
}
else if (choice == 3)
{
}
和choice
未定义
你如何解决尽可能多的错误信息,然后告诉我们那些困扰你的具体信息。