c++ - This while loop does its proper function, but required for something else? -
i making hotel reservation program in c++. when user wants open guest's info file, have input first name , surname of guest. if there isn't data file name in it, tells user file doesn't exist. asks again file name, , user inputs real file name.
the problem if inputs file name know real, file doesn't exist. if re-enter file name when prompted, loads file.
by way, loop required file open. that's main thing i'm stumped about. tried messing around see if needed it, , apparently. , need first file_ptr.open(filename,ios::in);
, along second. don't understand why need both.
here specific code while loop:
cout << "open member file"; system ("cls"); char filename [100]; ifstream file_ptr; cout << "\n\t\t\t\tsaved members:\n\n"; system ("dir/b *."); cout << "\n\nplease type name of member you\n"; cout << " wish open appears above or\n"; cout << " type z (lower case) return main menu: "; cin.ignore(); gets (filename); if (filename[0] != 'z') { //cout<< "\nplease enter 6 digit registration access code: "; //cin>>rac; //if (rac == (file_ptr, membershipnumber)) //{ file_ptr.open(filename,ios::in); // char in_char; while(!file_ptr) { cout << "member not exist\n"; gets (filename); file_ptr.open(filename,ios::in); }
i don't understand why while loop required open file if user gave legit file name. says file doesn't exist, opens file on second try.
this output:
saved members:
hunter grad
please type name of member wish open appears above or type z (lower case) return main menu: hunter grad
member not exist
the second time input same file name, takes me file
i don't understand why though? hoping know. thanks
http://www.cplusplus.com/reference/istream/istream/ignore/
you tell input stream ignore 1 character, filename not typed. if typed "john" try open file "ohn".
if had looked @ variable in debugger or checked output, have been obvious. debugger far more effective @ solving problems stackoverflow. want come here when stuck.
the second time type in "john" file named "john", since there no ignore in loop.
Comments
Post a Comment