mardi 29 septembre 2015

Else component of if/else not executing (C++)

I have a relatively large program and not all of it is relating to my question, but this particular bit is stumping me. Below is my int main:

int main ()
{
    int caseNumber ;
    string clientName, clientEmail, subject, path, fileName, firstTime ;
    //creates string for path of files created and where the Python project is stored (with the .exe)
        path = _pgmptr ;
        fileName = "EmailGenerator.exe" ;
        fileName.length() ;
        path.erase(path.length() - fileName.length()) ;

    //checks first time use for customizing
        cout << "Welcome to the Ticket Email Generator.\n\n" 
            << "If this is your first time using this program,\nplease enter Y to customize some personal details.\n" 
            << "If not, enter any other character.\n" ;
        cin >> firstTime ;
        cin.ignore() ;
        if (firstTime == "Y" || firstTime == "y")
        {
            //save sender email (defaults to morgan_wallace@cable.comcast.com - creator)
            setSender (path) ;

            //Verifies signature file is as desired (to be saved for the future)
            char ready = 'n' ;
            while (!(ready == 'y' || ready == 'Y')) 
            {
                std::cout << "\nPlease modify the signature.txt file located in " + path << endl
                        << "Enter Y when done.\n" ;
                cin >> ready ;
                cin.ignore() ;
            }
        }

    //Email "To" field:
        setTo (path) ;

    //Email "Subject" field:
        setSubject (path) ;

    //Email "Body" field:
        std::cout << "\nPlease provide the following information for the body of the email:" << endl ;
        //create standard time-based greeting at top of message
            setToName (path) ;
        //select message type & compose body of message
            ofstream scriptfout (path + "script.txt") ;
            std::cout << "\nPlease select from case type menu:" << endl << endl ;
            caseTypeMenu(scriptfout) ;
            scriptfout.close() ;
        //compose full body and add signature
            setBody (path) ;

    std::cout << "Would you like to review your email before sending? Y/N  " ;
    char reviewChar ;
    cin >> reviewChar ;
    cin.ignore() ;
    if (reviewChar == 'y' || reviewChar == 'Y')
    {
        review (path) ;
    }
    else if (reviewChar == 'N' || reviewChar == 'n')
    {
        //run email sender (python program)
                string pythonPathString = path + "EmailProj/EmailProj/EmailProj.py" ;
                string pythonString = "C:/Python27/python.exe " + pythonPathString + " " + path ;
                system(pythonString.c_str()) ;
        //Exit program
                string anything ;
                std::cout << "Enter anything to exit\n" ;
                cin >> anything ;
                cin.ignore() ;
    }
    else
    {
        cout << "Invalid entry review: " + reviewChar << endl ;
        //Exit program
                string anything ;
                std::cout << "Enter anything to exit\n" ;
                cin >> anything ;
                cin.ignore() ;
    }

    return 0 ;
}

My specific issue my last if/else (everything else executes as expected):

        std::cout << "Would you like to review your email before sending? Y/N  " ;
        char reviewChar ;
        cin >> reviewChar ;
        cin.ignore() ;
        if (reviewChar == 'y' || reviewChar == 'Y')
        {
            review (path) ;
        }
        else if (reviewChar == 'N' || reviewChar == 'n')
        {
            //run email sender (python program)
                    string pythonPathString = path + "EmailProj/EmailProj/EmailProj.py" ;
                    string pythonString = "C:/Python27/python.exe " + pythonPathString + " " + path ;
                    system(pythonString.c_str()) ;
            //Exit program
                    string anything ;
                    std::cout << "Enter anything to exit\n" ;
                    cin >> anything ;
                    cin.ignore() ;
        }
        else
        {
            cout << "Invalid entry review: " + reviewChar << endl ;
            //Exit program
                    string anything ;
                    std::cout << "Enter anything to exit\n" ;
                    cin >> anything ;
                    cin.ignore() ;
        }

If you input an 'h', or some character not Y, y, N, or n, it does not print the error script with the inappropriate answer. It prints a line of my code (something that is printed to screen with cout in an earlier portion of the code) and then "Enter anything to exit" and waits for input. If you input 'g', it just skips the error message altogether and prints the "Enter anything to exit line" and waits for input.

The point of this last if/else is to allow the user to view their message, and then decide whether to send it, edit portions of it, or just disregard it completely. For purposes of error checking, I would like to handle inputs that are not Y, y, N, and n, even though theoretically there should be no other inputs.

Aucun commentaire:

Enregistrer un commentaire