在结构体中搜索字符串
好吧,我在让它工作时遇到问题,所以有人可以帮我解决这个问题吗?我正在尝试要求用户搜索电台名称,如果找到,则输出它,如果没有,则退出。
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;
}
}
Ok I having a Problem Getting this to Work, So could some one please help me with this I am Trying to ask a user to Search for a Station Name and if its is found output it if not then exit..
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在几个地方,您似乎有赋值(单个
=
),您打算在其中测试相等性(双==
),如下例所示:此外,
Station_Origin
和intermediate_station1/2/3
定义为std::string
,而origin
定义为int< /代码>。您正在尝试将
string
分配或比较int
。In several places it looks like you have assignment (single
=
) where you meant to test for equality (double==
) like the following example:In addition,
Station_Origin
andintermediate_station1/2/3
are defined asstd::string
whileorigin
is defined as anint
. You're attempting to assign or compare astring
to anint
.您没有告诉我们发生了什么,所以我们只能通过扫描代码来猜测。让我印象深刻的一件事是:
choice
是未定义的。您如何解决尽可能多的错误消息,然后告诉我们那些困扰您的具体错误消息。
You haven't told us what is happening, so we can only guess, by scanning over the code. One thing that strikes me is:
and
choice
is undefined.How about you solve as many error messages as possible, and then tell us the specific ones that are stumping you.