1

I am trying to read in a .csv file of hotel room reservations. The objects are created correctly, but I am getting an out of range error after the reading in is completed.

I have tried messing around with inFS.ignore() in various places, but it will either mess up some/all inputs alone or that and throw the same error. The only time I can get all correct values is when I omit it completely (as shown above) but then I get this error again. I am confused because im using push_back for the vector so that shouldn't be a range issue. Additionally, I have while loop running while !eof() so I don't think its a file issue either

This is the method I have created to open & read the file

void openFile(string fileName, Hotel h){
ifstream inFS;
inFS.open(fileName);
if (!inFS.is_open()){       
    cout << "File error;" << endl;
    return;
}
else {
    string date;
    string type;
    string name;
    string email;
    string op1;
    string op2;
    getline(inFS, date);
    while(!inFS.eof()){
        getline(inFS, type, ',');
        getline(inFS, name, ',');
        getline(inFS, email, ',');
        getline(inFS, op1, ',');
        getline(inFS, op2, ',');
        char o1 = op1.at(0);
        char o2 = op2.at(0);

        char t1 = type.at(0);                   
        if(t1 == BUNGALOW){
            Bungalow bRoom;
            bRoom.setRoomType(type);
            bRoom.setCustomerName(name);
            bRoom.setCustomerEmail(email);
            bRoom.setViewChoice(o1);
            bRoom.setHoneymoonPackage(o2);
            Room* pointer = &bRoom;
            h.addRoom(pointer);
            cout << "Bungalow created" << endl;
        }
        else if(t1 == KING){
            DeluxeKing kRoom;
            kRoom.setRoomType(type);
            kRoom.setCustomerName(name);
            kRoom.setCustomerEmail(email);
            kRoom.setConciergeFloor(o1);
            kRoom.setParkingGarageAccess(o2);
            Room* kPointer = &kRoom;
            h.addRoom(kPointer);
            cout << "Deluxe King created" << endl;
        }
        else if (t1 == QUEEN){
            StandardQueen qRoom;
            qRoom.setRoomType(type);
            qRoom.setCustomerName(name);
            qRoom.setCustomerEmail(email);
            qRoom.setLargerRoom(o1);
            qRoom.setParkingGarageAccess(o2);
            Room* qPointer = &qRoom;
            h.addRoom(qPointer);
            cout << "Standard King created" << endl;
        }
        else {
            cout << "Received: " << t1 << endl;
        }


    }           


}

} Here is there csv file I am using:

12/01/20
B01,John Smith,jsmith@jsnet.com,B,H
B04,Alan Wade,aw@aw.net,G,X
DK03,Susan Slate,susie@slate.com,Y,Y
DK07,Tim Tolee,tolee@tim.net,N,Y
Q09,Nicole Thomas,nthom@xxx.com,Y,Y
Q10,Brown Dole,drdo@dole.net,N,Y
  • 1
    Use the debugger and look at the call stack when that error occurs. Second, please post a [mcve] -- we do not know what `addRoom` is. Last, you are storing the address of local variables, such as `Room* pointer = &bRoom;`. What happens when that local variable goes out-of-scope? – PaulMcKenzie Dec 02 '20 at 22:00
  • 1
    *I have tried messing around with inFS.ignore() in various places* -- Honestly, I think you should concentrate on writing the C++ program properly, rather than things like `ignore`. Your program contains more important issues and mistakes. – PaulMcKenzie Dec 02 '20 at 22:02
  • Seems likely that one of you calls to `at(0)` is failing because the string concerned has zero length. That's what the error says. It's hard to make any more suggestions though because we cannot see the input you are using. If we could see the input we could make suggestions on how to improve the code. – john Dec 02 '20 at 22:10
  • 1
    However `while(!inFS.eof()){` is wrong, and could be the cause of the problem, see here https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – john Dec 02 '20 at 22:12
  • Just read this `while !eof() so I don't think its a file issue` Quite a few beginners think `while (!stream.eof())` does something different from what is actually does. Please read the above link carefully to understand how you should really read from a file. – john Dec 02 '20 at 22:20
  • 1. Why reinvent the wheel? Use a CSV reader library. 2. You have a bunch of duplicated code in there. Can't you use the `room.setWhatever()` _outside_ of those blocks? 3. Consider having your room class be merely a facade for a raw table record. – einpoklum Dec 02 '20 at 22:32
  • Even though some code is missing, we can guess that your code is probably incorrectly add pointer to local variables inside function `addRoom`. if so, you have **undefined behavior** and you should remove any undefined behavior first as sometime it cause really strange behavior that is hard if not impossible to explain! – Phil1970 Dec 03 '20 at 02:21

0 Answers0