0

I want to check if the user inputs a number that is the same as in my drivers.txt file. The file looks like this:

Angela  07483774637 1000    1100
Mark    07362543432 1100    
Dave    07869878907 1200    1100

Name, number, and time booked for all the drivers. Check if the user inputs a time that is the same as the last 2 columns. If that time is booked up on all drivers (i.e. if all drivers have 1100 next to their names) then say that a taxi is not available for that time and if the time is free on one, then say that a taxi is available.

Should know that as soon as it finds one instance of the same number as the user then does not say that a taxi is available for that time (e.g. checks column 1 first then if it has that number then do not continue (i.e. Mark has 1100, but the next column is blank so program may think that Mark is available for 1100)). Later I will implement a writing to file function so that the users write to the file the times booked instead of me writing them manually.

userTime2 = the users input

The relevant code so far:

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

const int MAXDRIVERS = 3;
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 < MAXDRIVERS; 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();
                }
            }
        }

    }

What do I need to add/change to this to make it work? Thank you.

lydia4444
  • 1
  • 2
  • This is pretty broad. As-is, it sounds like you want people to finish your homework for you. What specifically are you having problems with? – Carcigenicate Apr 10 '17 at 21:31
  • It will not read the file correctly. When debugging, it for some reason assigns the 'timeBooked' value to 'firstName' ? – lydia4444 Apr 10 '17 at 21:37
  • 1
    Possible duplicate of [Read file line by line](http://stackoverflow.com/questions/7868936/read-file-line-by-line). Specifically Answer 1 option 2. – user4581301 Apr 10 '17 at 21:39
  • sorry i am new to c++. i just tried using that but i am unsure why it wont let me cout << a and cout << b ? Also i have no idea how to set it so that it skips the first 2 columns and only reads the last two – lydia4444 Apr 10 '17 at 21:51

1 Answers1

0

It assigns the "timeBooked" value to 'firstName' because you are using tokens to read from the file and this is what is happening when it reads the file Angela is assigned to firstName[0], 0748... to number[0], 1000 to timeBooked[0], and last 1100 to firstName[1]. I hope this helps.

msfs
  • 11
  • 2
  • sorry im not sure what you mean – lydia4444 Apr 11 '17 at 12:39
  • inFile >> cab.firstName[i] >> cab.number[i] >> cab.timeBooked[i]; Angela is assigned to cab.firstName[0], 07483774637 to cab.number[0] , 1000 to cab.timeBooked[0], and then i increments to 1 since not everything in the line was read cab.firstName[1] is 1100. you have to be really careful with token reads everything in the file has to be right. If you are still confused I suggest googling tokenized file reads. – msfs Apr 12 '17 at 02:39