我有这个功能,应该读取到文本文件的末尾,以检索数据并将数据存储在某些变量中。
dd dd dd 2 350102 RIC Thing美国2 350104
然后,我使用另一个功能来尝试修改数据。您可以看到变量在哪里有数据,但是当我尝试比较数据时会说找不到该数据。
发现104
请输入您要更改的房间号:104
这两个被打印出来,所以我可以看到检索到的内容 102 104
RIC Thing USA 2350104
但找不到102。
请输入您要更改的房号:102
这两个被打印出来,所以我可以看到检索到的内容 102 104
抱歉...找不到匹配的内容。
请确保已预订房间号,然后才能对其进行修改。
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();
}
//}
}
答案 0 :(得分: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