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
}
Programming Languages-OtherC++

Avatar of undefined
Last Comment
jkr

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
jkr

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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?
jkr

Sorry, missed that - check for

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

Open in new window

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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
jkr

You're most welcome ;o)