if / else不执行的其他组件(C ++)

时间:2015-09-30 00:11:35

标签: c++ if-statement

我有一个相对较大的计划,并不是所有的都与我的问题有关,但这一点特别让我感到难过。 下面是我的主要内容:

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 ;
}

我的特定问题是我的最后一个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 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() ;
        }

如果输入'h'或某些字符不是Y,y,N或n,则不会使用不正确的答案打印错误脚本。它会打印一行我的代码(在代码的前面部分用cout打印到屏幕上),然后“输入任何东西退出”并等待输入。如果你输入'g',它只是完全跳过错误信息并打印“输入任何东西到退出行”并等待输入。

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

1 个答案:

答案 0 :(得分:4)

你的问题在这里:

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

使用加号时,实际上是将一个整数的值添加到指针中,而不是连接字符串。

试试这个:

cout << "Invalid entry review: " << reviewChar << endl ;
相关问题