【问题标题】:C++ How to check if user input is the same as in a file?C ++如何检查用户输入是否与文件中的相同?
【发布时间】:2017-04-06 21:24:34
【问题描述】:

我想检查用户输入的值是否在我打开的文件中。

我正在做一个出租车预订系统。

用户输入 = userTime(用户想从出租车上取车的时间)

我正在尝试检查这个时间是否已经在所有驱动程序旁边 (4)

在我的文件中 司机姓名,
司机号码,
预约时间

如果 userTime 与文件中所有 4 个驱动程序的预订时间相同,则系统将显示在所需时间内没有可用驱动程序

它是 24 小时制,因此 userTime 将与预定时间的文件内容完全匹配。例如 1200 = 12pm

请尽快提供帮助,谢谢我是 C++ 新手

到目前为止,这是我的代码,一个函数。它在 cab 的第二个实例上说“表达式必须具有指向对象类型的指针”。还有 cab.timeBooked 上的两个 [i]。请问有什么问题?

struct Driver
{
    string firstName;
    double number;
    int timeBooked;
};


struct Driver cab;  

void searchDrivers()
    {
        cout << "\nSearching for drivers..." << endl;
        ifstream inFile;
        inFile.open("drivers.txt");
        if (!inFile)
        {
            cout << "error reading file";
        }
        else
        {
            for (int i = 0; i < 4; i++)
            {
                inFile >> cab.firstName[i] >> cab.number[i] >> cab.timeBooked[i];
                if (userTime2 == cab.timeBooked[i])
                {
                    cout << "unavailable" << endl;
                }
                else
                {
                    cout << "car available" << endl;
                    driverIndex = i;
                    confirmBooking();
                }
            }
        }

    }

【问题讨论】:

    标签: c++


    【解决方案1】:

    你必须创建一个结构来相应地读取和存储所有内容,然后你可以将它与用户输入进行比较

    结构看起来像这样

    struct foo 
    {
    string name ; 
    long number ; 
    int time ; 
    }
    

    然后你可以这样比较

     foo e ; 
     // read from file and store in struct 
        if ( userinput == e.time ) 
          // do something 
    

    ================================================ =========================== 我已经为 1 个驱动程序解决了这个问题,你可以弄清楚如何处理 4 个

    #include <iostream>
    #include <fstream>
    #include <conio.h>
    
    using namespace std;
    
    struct driver_data
    {
        string name;
        size_t number;
        int time;
    };
    
    void main ()
    {
        ifstream myfile ;
        myfile.open("data.txt");
    
        if ( ! myfile )
        {
            cout <<"error in opening file";
        }
    
        driver_data e1 , e2 , e3 ;
    
        while ( ! myfile.eof() )
        {
            char ch[100];
            myfile.getline (ch,100,'\n');
            e1.name = ch ;
            myfile.getline (ch,100,'\n');
            e1.number = atoi( ch  );
            myfile.getline(ch , 100 , '\n');
            e1.time = atoi ( ch );
        }
    
        int time_input_by_user ;
    
        cout <<"enter the time you want the cab to arrive"<<endl ;
        cin >> time_input_by_user ;
    
        if (time_input_by_user == e1.time )
            cout<<"car not available"<<endl;
        else 
            cout<<"car available we'll be right on time"<<endl;
    
        getch();
    }
    

    我存储了一个文件(命名数据),其中包含以下数据。

    john
    2132151123
    1200
    mark
    5121421231
    1100
    wayne
    151231231
    1000
    harry
    215612312
    1500
    

    【讨论】:

    • 您好,感谢您的回复,我似乎无法找到将文件读入结构的任何内容,您能否建议我如何执行此操作?
    • @L.Lane 我更新了答案,您可以将其标记为正确答案(如果您发现它解决了您的问题),因此可以关闭问题
    • 这段代码充满了错误(while(!eof())void main()),C-isms(char[]atoi),过时的不可移植的东西(conio.hgetch ) 和通常糟糕的using namespace std;。 DV。
    • @Quentin 首先我完全同意using namespace std是一种不好的做法,其次我知道使用conio.hgetch使程序不可移植,但我还没有找到替代方案(如何停止屏幕?使用cin?),第三(while(!eof()), void main())有什么问题。我知道 atoi 是一个 c 函数,但它有什么问题,char[] 有什么问题不反对任何只是想学习的东西
    • 暂停程序:std::istream::ignore; while(!eof) loops once too many; main must return int; std::atoi's error checking is broken,当我们有std::string 时,摆弄char[] 是没有意义的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-20
    • 2020-07-01
    • 2021-03-21
    相关资源
    最近更新 更多