-7

I have this function that is supposed to read to the end of a text file retrieve the data and store the data in some variables.

First Name-Last Name-Nationality-Guest(s)-Cost-Room #

dd dd dd 2 350 102 RIC Thing USA 2 350 104

I then use another function to try and modify the data. You can see where the variable has the data but when I'm trying to compare the data is say it is not found.

It found 104

Please enter the room number you would like to make the changes to : 104

First Name-Last Name-Nationality-Guest(s)-Room #

These two were printed so I could see what was retrieved 102 104

RIC Thing USA 2 350 104

But not finding 102.

Please enter the room number you would like to make the changes to : 102

First Name-Last Name-Nationality-Guest(s)-Room #

These two were printed so I could see what was retrieved 102 104

SORRY...NO MATCH FOUND.

Please ensure that the room number typed was was booked before it can be modified.

void HotelRoom::modifyresroom()
{
    int occup;
    string roomtochange;
    string guestroomdb;
    int newaccupancy;
    double newcost;
    char savinf;
    string fname, lname, nationality;
    string checkaddroom;
    ifstream getdatafromaddroom; // creation of the ifstream object
    getdatafromaddroom.open("reserveroom.out");

    cout << "Please enter the room number you would like to make the changes to : ";
    cin >> roomtochange;

    cout << endl;

    if (getdatafromaddroom.fail()) // if statement used for error
    // checking
    {
        cout << "Could not open file" << endl; // message that will be
    }
    cout << "First Name" << '-' << "Last Name" << '-' << "Nationality" << '-' << "Guest(s)" << '-' << "Room #" << endl;
    cout << "-------------------------------------------------------" << endl;

    while (!getdatafromaddroom.eof()) {
        getdatafromaddroom >> fname >> lname >> nationality >> occup >> cost >> guestroomdb;
        if (getdatafromaddroom.eof()) {
            break;
        }
        cout << guestroomdb << endl;
    }
    //if statement begin by error checking
    if (roomtochange != guestroomdb) {

        cout << "SORRY...NO MATCH FOUND." << endl;
        //cout << setw(5) << fname << ' ' << setw(10) << lname << ' ' << setw(10) <<nationality << ' ' << setw(10) << occup<< ' ' << setw(9) << cost << ' ' << setw(9) << guestroomdb <<endl;
        char backtomod;
        cout << endl;
        cout << "Please ensure that the room number typed was  was booked before it can be modified." << endl;
        cout << endl;
        cout << "1 - Enter another room number, 2 - Back to the mail menu : ";
        cin >> backtomod;

        //switch block used to allow the user to reenter the room number or go back to the mail menu
        switch (backtomod) {
        case '1':
            HotelRoom::modifyaddromm();
            break;
        case '2':
            HotelRoom::hotelmenu();
            break;
        default:
            cout << endl;
            cout << "(--" << backtomod << "--)"
                 << " wasn't a valid selection. you will now be directed to the main menu.";
            system("Pause");
            cout << endl;
            HotelRoom::hotelmenu();
            break;
        }
    }
    else {

        // if room number is found t will be printed out below

        cout << setw(5) << fname << ' ' << setw(10) << lname << ' ' << setw(10) << nationality << ' ' << setw(10) << occup << ' ' << setw(9) << cost << ' ' << setw(9) << guestroomdb << endl;
    }

    cout << "How many person should be in the new room? : ";
    cin >> newaccupancy;
    newcost = newaccupancy * HotelRoom::getdailyrate();
    cout << endl;
    cout << "[y] to save : ";
    cin >> savinf;

    cout << fname << ' ' << lname << ' ' << nationality << ' ' << newaccupancy << ' ' << newcost << ' ' << guestroomdb << endl;
    if (savinf == 'y' || savinf == 'Y') {
        ofstream editrecord;
        editrecord.open("reserveroom.out", ios::app);
        editrecord << fname << ' ' << lname << ' ' << nationality << ' ' << newaccupancy << ' ' << newcost << ' ' << guestroomdb << endl;
        editrecord.close();
        getdatafromaddroom.close();
        hotelmenu();
    }
    else {
        cout << "The changes made weren't saved.";
        system("Pause");
        hotelmenu();
    }
    //}
}
Dray Mck
  • 9
  • 3
  • 1
    Please do not post pictures of text. – Nicol Bolas Nov 23 '18 at 15:44
  • while (!getdatafromaddroom.eof()) https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Nov 23 '18 at 15:48
  • Thank you for the link. Can you tell me why even though the data was retrieved it is saying that is wasn't found – Dray Mck Nov 23 '18 at 15:55
  • 1
    You have a loop that reads through the whole file (more or less). Only the last item in the file will be preserved in memory, the rest will have been overwritten by subsequent reads. I'd start investigating the bug here. – user4581301 Nov 23 '18 at 16:09
  • In the loop that is supposed to read all records from the file you keep overwriting the same variables again and again. Only the last record will remain in them, the others are gone. You need some way to keep all records if you want to find some data in them later. – Swordfish Nov 23 '18 at 16:10
  • ok. Thank you. So why would it still print out the data as if it is there? – Dray Mck Nov 23 '18 at 16:11
  • Because you print it inside the loop. In the next iteratiion, `guestroomdb` gets overwritten by the next input operation. – Swordfish Nov 23 '18 at 16:12

1 Answers1

2
bool found = false;
while (getdatafromaddroom >> fname >> lname >> nationality >> occup >> cost >> guestroomdb) {
    if (guestroomdb == roomtochange) {  // stop when you found the
        found = true;                   // record you are interested in
        break;
    }
}

if (!found) {  // found is still false? No such record in the file.
    // handle error
Swordfish
  • 1
  • 3
  • 17
  • 42