locked
vectors RRS feed

  • Question

  • hey guys im trying to implement vectors but it find that my code wont read the data from the file into the vevtor

     

     

    void loadFiles()   //changes made

    {

    fstream file;

    file.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );

    roomType tempRoom;

    try

    {

    int count = 0;

    file.open("GuestFile.txt");

     

    if (!file.eof())

    file >> count;

    for (int i = 0; i < count; i++)

    {

    int tempID;

    string tempFirst;

    string tempLast;

    char temp[250];

    string Address;

    long long phone;

    string CCNum;

    double tempBalanceDue;

    string CCType;

    string Category;

    string IDType;

     

    file >> tempID;

    file >> tempFirst;

    file >> tempLast;

    file.ignore();

    file.getline(temp, 250);

    Address = static_cast<string>(temp);

    file >> phone;

    file >> CCNum;

    file >> CCType;

    file >> tempBalanceDue;

    file >> Category;

    file >> IDType;

    Guest tempGuest;

    tempGuest.SetGuest(tempFirst, tempLast, Address, phone, CCNum, CCType, tempID, tempBalanceDue,Category,IDType);

    Guests.push_back(tempGuest);

    }

    file.close();

    }

    catch(exception &e)

    {

    cout << e.what() << " +Error trying to open GuestFile.txt" << endl;

    file.clear();

    system("pause");

    }

    try

    {

    int count = 0;

    file.open("roomFile.txt");

     

    if (!file.eof())

    file >> count;

    for (int i = 0; i < count; i++)

    {

    int roomNo;

    double cost;

    bool smoking, twobeds, suite;

    string status;

     

    file >> roomNo;

    file >> cost;

    file >> smoking;

    file >> twobeds;

    file >> suite;

    file >> status;

    roomType temp(roomNo, cost, twobeds, smoking, suite,status);

    rooms.push_back(temp);

    }

    file.close();

    }

    catch(exception &e)

    {

    cout << e.what() << " +Error trying to open roomFile.dat" << endl;

    file.clear();

    system("pause");

    }

     

    try

    {

    int count = 0;

    file.open("reservationFile.txt");

     

    if (!file.eof())

    file >> count;

    for (int i = 0; i < count; i++)

    {

    int IDNum;

    int reservNo;

    int roomNo;

    double cost;

    int day1, month1, year1, day2, month2, year2;

     

    file >> IDNum;

    file >> reservNo;

    file >> roomNo;

    file >> cost;

    file >> day1 >> month1 >> year1;

    file >> day2 >> month2 >> year2;

     

    HotelReservation temp(IDNum, roomNo, reservNo, cost, day1, month1, year1, day2, month2, year2);

    reservations.push_back(temp);

    }

    file.close();

    }

    catch(exception &e)

    {

    cout << e.what() << " +Error trying to open reservationFile.txt" << endl;

    file.clear();

    system("pause");

    }

    try

    {

    int count = 0;

    file.open("VehicleFile.txt");

     

    if (!file.eof())

    file >> count;

    for (int i = 0; i < count; i++)

    {

    int tempVID;

    string tempVLPN;

    string tempVmake;

    string tempVmodel;

    string tempVtype;

    string tempVcolor;

     

    file >> tempVID;

    file >> tempVLPN;

    file >> tempVmake;

    file >> tempVmodel;

    file >> tempVtype;

    file >> tempVcolor;

     

    Vehicle tempVehicle(tempVLPN,tempVmake,tempVmodel,tempVtype,tempVcolor,tempVID);

    Vehicles.push_back(tempVehicle);

    }

    file.close();

    }

    catch(exception &e)

    {

    cout << e.what() << " +Error trying to open VehicleFile.txt" << endl;

    file.clear();

    system("pause");

    }

    }

    Thursday, November 25, 2010 9:50 PM

Answers

  • I must say this code looks very strange to me:

    >fstream file;
    >file.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
    >roomType tempRoom;
    >try
    >{
    >int count = 0;
    >file.open("GuestFile.txt");
    >if (!file.eof())
    >file >> count;

    (1) Why would you be checking for eof immediately after
    opening the file? When reading a file the eof bit gets set
    when an attempt is made to read *past* the end of the file
    data.

    (2) Testing for fail() is a more conventional test to
    make immediately after attempting an open.

    (3) Regardless of whether you check for eof or fail after
    opening, if that condition is true the code in the
    conditional block will never get executed. It is inside
    a try {} block and you have requested an exception be
    thrown if the failbit or eofbit is set. If the file fails
    to open, an exception will occur immediately on the open()
    and control will be transferred immediately to the catch()
    block. All instructions in the try{} block after the open()
    will be skipped if the open() fails and an exception is
    thrown.

    - Wayne

    • Marked as answer by lucy-liu Thursday, December 2, 2010 4:06 AM
    Friday, November 26, 2010 2:40 AM

All replies

  • So what happens? Use the debugger to single-step through your code and see what happens. Are the variables filling up with the expected values? If not, compare the values with what is in your input file.

    Thursday, November 25, 2010 9:58 PM
  • yea i checked it and its not filling up i did the step into and found that count isn't getting the value its supposed to get from the file which is 10 i am currently checking it now
    Thursday, November 25, 2010 10:40 PM
  • On 25/11/2010 23:40, Jblacks wrote:

    yea i checked it and its not filling up i did the step into and found that count isn't getting the value its supposed to get from the file which is 10 i am currently checking it now

    You're checking the reading code, but you should check also the writing code.
    In fact, could it be possible that 'count' is not getting the value its supposed to get from file because the file content is wrong or just different from what you expected?

    Giovanni

    Thursday, November 25, 2010 11:11 PM
  • it was actually the file.exceptions part which wasn't working or i should say was causing the files not to be opened after commenting it out and running the program it worked like a charm. Currently checking why that is so
    Friday, November 26, 2010 2:17 AM
  • I must say this code looks very strange to me:

    >fstream file;
    >file.exceptions ( ifstream::eofbit | ifstream::failbit | ifstream::badbit );
    >roomType tempRoom;
    >try
    >{
    >int count = 0;
    >file.open("GuestFile.txt");
    >if (!file.eof())
    >file >> count;

    (1) Why would you be checking for eof immediately after
    opening the file? When reading a file the eof bit gets set
    when an attempt is made to read *past* the end of the file
    data.

    (2) Testing for fail() is a more conventional test to
    make immediately after attempting an open.

    (3) Regardless of whether you check for eof or fail after
    opening, if that condition is true the code in the
    conditional block will never get executed. It is inside
    a try {} block and you have requested an exception be
    thrown if the failbit or eofbit is set. If the file fails
    to open, an exception will occur immediately on the open()
    and control will be transferred immediately to the catch()
    block. All instructions in the try{} block after the open()
    will be skipped if the open() fails and an exception is
    thrown.

    - Wayne

    • Marked as answer by lucy-liu Thursday, December 2, 2010 4:06 AM
    Friday, November 26, 2010 2:40 AM
  • Hi Jblacks,

    I temporary make WayneAKing's reply as answer. You can unmark it if they provide no help.

    Best Regards,

    Lucy

    Thursday, December 2, 2010 4:06 AM