【问题标题】:Else component of if/else not executing (C++)if/else 的其他组件未执行 (C++)
【发布时间】:2023-03-16 19:04:01
【问题描述】:

我有一个相对较大的程序,并不是所有的程序都与我的问题有关,但是这个特殊的部分让我很困惑。 以下是我的主要内容:

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 的输入,即使理论上不应该有其他输入。

【问题讨论】:

  • 你能在调试器下运行它并单步运行它以查看问题所在吗?这方式比观察和猜测方法更有效。
  • 我认为你的表达,"Invalid entry review: " + reviewChar 是无效的。我不认为 C++ 会自动将 reviewChar 转换为字符串来连接它。字符串和字符是两种不同的数据类型。试试这个:cout &lt;&lt; "Invalid entry review: " &lt;&lt; reviewChar &lt;&lt; endl;.
  • 现在我觉得很傻 - 将其更改为 '
  • 一点点 C++ 继承自 C:有时当您尝试做一些愚蠢的事情并且不会告诉您时,它会假设您是认真的。 ;)

标签: c++ if-statement


【解决方案1】:

你的问题在这里:

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

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

试试这个:

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

【讨论】:

  • 由 lurker 添加,这解决了问题。当我被允许时,我会设置为答案。
猜你喜欢
  • 2021-12-21
  • 2021-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 2012-11-02
  • 1970-01-01
相关资源
最近更新 更多