Read text file to end in c++ [closed]












-6














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();
}
//}
}









share|improve this question















closed as unclear what you're asking by Neil Butterworth, Nicky C, Cindy Meister, user4581301, πάντα ῥεῖ Nov 23 '18 at 16:36


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Please do not post pictures of text.
    – Nicol Bolas
    Nov 23 '18 at 15:44










  • while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
    – 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
















-6














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();
}
//}
}









share|improve this question















closed as unclear what you're asking by Neil Butterworth, Nicky C, Cindy Meister, user4581301, πάντα ῥεῖ Nov 23 '18 at 16:36


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1




    Please do not post pictures of text.
    – Nicol Bolas
    Nov 23 '18 at 15:44










  • while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
    – 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














-6












-6








-6







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();
}
//}
}









share|improve this question















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();
}
//}
}






c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 16:01

























asked Nov 23 '18 at 15:42









Dray Mck

12




12




closed as unclear what you're asking by Neil Butterworth, Nicky C, Cindy Meister, user4581301, πάντα ῥεῖ Nov 23 '18 at 16:36


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Neil Butterworth, Nicky C, Cindy Meister, user4581301, πάντα ῥεῖ Nov 23 '18 at 16:36


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    Please do not post pictures of text.
    – Nicol Bolas
    Nov 23 '18 at 15:44










  • while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
    – 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














  • 1




    Please do not post pictures of text.
    – Nicol Bolas
    Nov 23 '18 at 15:44










  • while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
    – 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








1




1




Please do not post pictures of text.
– Nicol Bolas
Nov 23 '18 at 15:44




Please do not post pictures of text.
– Nicol Bolas
Nov 23 '18 at 15:44












while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
– drescherjm
Nov 23 '18 at 15:48




while (!getdatafromaddroom.eof()) stackoverflow.com/questions/5605125/…
– 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




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




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




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




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












1 Answer
1






active

oldest

votes


















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





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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





    share|improve this answer


























      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





      share|improve this answer
























        2












        2








        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 16:18









        Swordfish

        8,95711335




        8,95711335















            Popular posts from this blog

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)