在结构体中搜索字符串

发布于 2024-12-12 01:32:07 字数 5817 浏览 0 评论 0原文

好吧,我在让它工作时遇到问题,所以有人可以帮我解决这个问题吗?我正在尝试要求用户搜索电台名称,如果找到,则输出它,如果没有,则退出。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-12-19 01:32:07

在几个地方,您似乎有赋值(单个 =),您打算在其中测试相等性(双 ==),如下例所示:

if (origin = myArray[n].Station_Origin)

此外,Station_Originintermediate_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:

if (origin = myArray[n].Station_Origin)

In addition, Station_Origin and intermediate_station1/2/3 are defined as std::string while origin is defined as an int. You're attempting to assign or compare a string to an int.

酒解孤独 2024-12-19 01:32:07

您没有告诉我们发生了什么,所以我们只能通过扫描代码来猜测。让我印象深刻的一件事是:

for(n=0; n < N_TRAIN; n++)
{
    ...
}      
else if (choice == 3)
{
}

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:

for(n=0; n < N_TRAIN; n++)
{
    ...
}      
else if (choice == 3)
{
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文