if/ Else的Else组件不执行(c++)

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

本文关键字:Else c++ 执行 组件 if      更新时间:2023-10-16

我有一个相对较大的程序,并不是所有的程序都与我的问题有关,但是这个特殊的部分难倒了我。下面是我的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.nn" 
            << "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 exitn" ;
                cin >> anything ;
                cin.ignore() ;
    }
    else
    {
        cout << "Invalid entry review: " + reviewChar << endl ;
        //Exit program
                string anything ;
                std::cout << "Enter anything to exitn" ;
                cin >> anything ;
                cin.ignore() ;
    }
    return 0 ;
}

我的特定问题我的最后一个if/else(其他所有内容都按预期执行):

        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 exitn" ;
                    cin >> anything ;
                    cin.ignore() ;
        }
        else
        {
            cout << "Invalid entry review: " + reviewChar << endl ;
            //Exit program
                    string anything ;
                    std::cout << "Enter anything to exitn" ;
                    cin >> anything ;
                    cin.ignore() ;
        }

如果您输入一个'h',或者不是Y、Y、N或N的字符,它不会打印错误脚本,其中包含不合适的答案。它打印我的一行代码(在代码的前面部分使用cout打印到屏幕上的代码),然后"输入任何内容以退出"并等待输入。如果你输入'g',它会直接跳过错误信息,输出"Enter anything to exit line",然后等待输入。

最后这个if/else的要点是允许用户查看他们的消息,然后决定是发送它,编辑它的一部分,还是完全忽略它。出于错误检查的目的,我希望处理不是Y、Y、N和N的输入,尽管理论上不应该有其他输入。

你的问题在这里:

cout << "Invalid entry review: " + reviewChar << endl ;

当你使用加号时,你实际上是在把一个整数的值加到一个指针上,而不是串接字符串。

试试这个:

cout << "Invalid entry review: " << reviewChar << endl ;