Link to home
Start Free TrialLog in
Avatar of ldken
ldken

asked on

user input file

Hi Experts,
Same computer class program, different problem... the program requires the user to provide either the input data file name or 'E' / 'e' to exit program. So far it will read and process the input string to exit but my problems is that if the input is a data file name it also goes into the  if ( userInFile == "E " || "e ") then just follows the same exit routine as if it were and E / e.
Here is how I have it coded.  Any suggestions will be greatly appreciated. ldken

void beginProgProcess(ifstream& inData, bool& yesEndProgram)
{
    bool isValid = true; // true = input selection ok, end loop
    string userInFile;

    while (isValid)
    {
        cout << endl << " Input datafile Name ... or 'E' to exit program ";
        getline(cin, userInFile);

        cout << " after cin input is " << userInFile << endl;
        system("pause");//------------------------------------------------------>prog check

        // user input to exit program
        if (userInFile == "E " || "e ")
        {
            cout << " after userInFile == E is " << userInFile << endl;
            system("pause");//-------------------------------------------------->prog check

            yesEndProgram = true;
            isValid = false;
        } // end if.

        // attempt to open user supplied data file name.
        else
        {
            cout << " just after else to open inData " << endl;
            system ("pause");//------------------------------------------------->prog check

            yesEndProgram = false; // do not end program as a file is provided.

            inData.open(userInFile.c_str());

            //test and warning message if open file does not exist.
            if(!inData)     // if file does not exist, clear, stmt to user, loop
            {
                inData.clear();

                cout << endl << "This Input file " << userInFile;
                cout << " does not exist. Try Again" << endl << endl;

                yesEndProgram = true;

                isValid = false;
            } // end if
            isValid = true;
        } //end else
    } // end while
}
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ldken
ldken

ASKER

The suggestion above corrected the problem with the if statement handling "E" and "e", however, it so far has not identified the input file name provided by user input. Any suggestions?
Sorry, missed that - check for

            if(!inData.is_open())     // if file does not exist, clear, stmt to user, loop
            {

Open in new window

Avatar of ldken

ASKER

Thanks for all your help, your suggestions along with setting the bool inValid flag correctly made the program work like it should. Yea!!!

Many thanks, ldken
You're most welcome ;o)