-3

I want to check if the value the user inputs is in the file that i have opened.

i am doing a taxi booking system.

user input = userTime (the time the user wants picking up from the taxi)

i am trying to check if this time is already next to all of the drivers (4)

in my file i have driver name,
driver number,
time booked

if userTime is the same as time booked on all 4 drivers in the file then the system will say no drivers available for the time required

it is in 24hr time so userTime would match exactly to the file contents in time booked. e.g 1200 = 12pm

please help soon thank you p.s. i am new to c++

here is my code so far, a function. it says 'expression must have pointer to object type' on the second instance of cab. and also the two [i]s on the cab.timeBooked 's. what is wrong with it please ?

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();
                }
            }
        }

    }
lydia4444
  • 1
  • 2

1 Answers1

0

you'll have to make a struct that will read and store everything accordingly and then you can compare it with the user input

the struct will look something like this

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

and then you can compare like this

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

========================================================================= I've solved this for 1 driver you can figure out how to deal with 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();
}

i had a file stored (named data) that had the following data.

john
2132151123
1200
mark
5121421231
1100
wayne
151231231
1000
harry
215612312
1500
RazaUsman_k
  • 694
  • 6
  • 20
  • 1
    hi thanks for the reply, i cant seem to find anything on reading a file into a struct, could you advise me on how to do this please? – lydia4444 Apr 06 '17 at 22:19
  • @L.Lane i updated the answer you can mark it as right answer ( jf you find it solving your problem ) so the question can be closed – RazaUsman_k Apr 07 '17 at 08:35
  • This code is rife with errors (`while(!eof())`, `void main()`), C-isms (`char[]`, `atoi`), obsolete non-portable stuff (`conio.h`, `getch`) and the usual awful `using namespace std;`. DV'd. – Quentin Apr 07 '17 at 08:57
  • @Quentin **firstly** i totally agree that `using namespace std` is a bad practice, **secondly** i do know that using `conio.h` and `getch` makes the program not portable but i haven't fount an alternative for that (how to stop screen ? using `cin` ? ) , **thirdly** what's wrong with `(while(!eof()), void main())`. I know `atoi` is a c function but what's wrong with that and what wrong with `char[]` **not objecting to anything just want to learn** – RazaUsman_k Apr 07 '17 at 09:41
  • 1
    Pausing the program: [`std::istream::ignore`](http://stackoverflow.com/questions/903221/press-enter-to-continue); [`while(!eof)` loops once too many](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong); [`main` must return `int`](http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main); [`std::atoi`'s error checking is broken](http://stackoverflow.com/questions/20583945/what-is-the-difference-between-stdatoi-and-stdstoi), and there's no point in fiddling with `char[]` when we have `std::string`. – Quentin Apr 07 '17 at 09:53
  • the code doesnt work, i debugged it and it is storing the time booked as the first name? so it just always says that there is a driver available – lydia4444 Apr 07 '17 at 15:15
  • like i said i only made this program for 1 driver. you'll have to think how to make it for 4 drivers Here at SO people won't solve whole programs for you. They'll just tell you what you are doing wrong and that is what this site is for – RazaUsman_k Apr 07 '17 at 15:24
  • yea i made it so i copied your exact code yet still doesnt work, as i was trying to see if the first taxi driver would work but it doesnt – lydia4444 Apr 07 '17 at 16:55
  • like quentin said it's not portable. What IDE are you using – RazaUsman_k Apr 07 '17 at 16:57
  • microsoft visual studio – lydia4444 Apr 07 '17 at 17:22
  • maybe you don't have the file in the same folder in which the project is – RazaUsman_k Apr 07 '17 at 17:34
  • it is in the same folder... i have no idea why it wont work – lydia4444 Apr 07 '17 at 17:49