2017-04-06 170 views
-3

我想检查用户输入的值是否在我打开的文件中。C++如何检查用户输入是否与文件中的输入相同?

我正在做的士预约系统。

用户输入= userTime

我试图检查,如果这个时候已经是旁边的所有驱动程序(用户希望从出租车拿起时间)(4)

在我的文件

我有 驱动程序名称,
司机数量,
时间预订

如果userTime相同预订了文件中的所有4个驱动程序时,则系统会说的时间没有可用的驱动程序需要

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

请尽快帮忙谢谢。我是新来的C++

这是我的代码到目前为止,一个函数。它在驾驶室的第二个实例中表示“表达式必须具有指向对象类型的指针”。还有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(); 
       } 
      } 
     } 

    } 

回答

0

你必须做一个结构,将读取并存储相应的一切,那么你可以这样

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 
+1

嗨,感谢您的回复,我似乎无法找到任何关于读取文件到结构中的任何内容,请问您如何做到这一点? – lydia4444

+0

@ L.Lane我更新了答案,您可以将其标记为正确答案(如果您发现它可以解决您的问题),因此可以关闭此问题 –

+0

此代码充满错误('while(!eof())''' void main()'),C-isms('char []','atoi'),过时的不可移植的东西('conio.h','getch')和通常糟糕的'using namespace std;'。 DVD。 – Quentin

相关问题