简体   繁体   English

警告C4715:“ bankingMenu”:并非所有控制路径都返回值(C ++)

[英]warning C4715: 'bankingMenu' : not all control paths return a value (C++)

this is my code for bankingMenu, i keep getting that error and I've done what i can to my knowledge to try and fix this but to no avail.. any suggestions? 这是我的bankingMenu的代码,我不断收到该错误,我已尽我所能尝试解决此问题,但无济于事。..有什么建议吗?

bool bankingMenu(bool menuFlag)  //banking menu 
{

    char choice;
    bool flag = false;
    bool flag2 = menuFlag;
    //print login menu
    cout<<"\n";
    cout<<"\n";
    cout<<" BANK MENU"<<endl;
    cout<<"----------------------------------------------------------------------"<<endl;
    cout<<endl;
    cout<<" A. SHOW ACCOUNT BALANCE"<<endl;
    cout<<" C. MAKE A WITHDRAWAL"<<endl;
    cout<<" D. WRITE A CHECK"<<endl;
    cout<<" E. SHOW ALL TRANSACTIONS"<<endl;
    cout<<" F. LOGOFF ACCOUNT"<<endl;

    cout<<"\n";
    cout<<"\n";
    cout<<"PLEASE ENTER YOUR SELECTION"<<endl;
    //enter choice of action
    cin>> choice;
    //change input to uppercase
    choice = toupper(choice);
    //input validation for choice
    while( flag == false)
    {
        if(choice != 'A' && choice != 'B' && choice != 'C' && choice!= 'D' && choice!= 'E' && choice!= 'F')
        {
            cout<<"\n";
            cout<<"\n";
            cout<<"ERROR!! YOU HAVE ENTERED AN INVALID CHOICE. PLEASE ENTER A VALID OPTION."<<endl;
            cin>> choice;
            //change input to uppercase
            choice = toupper(choice);
            continue;
        }
        flag=true;
    }
    //switch for action to be taken with the choice value
    switch (choice)
    {
    case 'A' : cout<< "YOU HAVE ENTERED A. \n";
        break;
    case 'B' : cout<< "YOU HAVE ENTERED B. \n";
        break;
    case 'C' : cout<< "YOU HAVE ENTERED C. \n";
        break;
    case 'D' : cout<< "YOU HAVE ENTERED D. \n";
        break;
    case 'E' : cout<< "YOU HAVE ENTERED E. \n";
        break;
    case 'F' : flag2= false;
        //returns to the login menu
        return flag2;
        break;
    default: ;
    }}

how can i fix this error? 我该如何解决此错误?

First, it's a warning, not an error. 首先,这是警告,而不是错误。 Your code will compile and run, but the warning is the compiler saying "This might not do what you thought it did! You might want to double-check it!" 您的代码将被编译并运行,但是警告是编译器说:“这可能不符合您的预期!您可能需要仔细检查!”

And the reason it says "not all control paths return a value" is that your function doesn't always return anything. 它之所以说“并非所有控制路径都返回值”,是因为您的函数并不总是返回任何值。 Specifically, it only returns anything if you enter 'F'. 具体来说,如果您输入“ F”,则仅返回任何内容。 To fix it, make your function always return something. 要修复它,使您的函数始终返回某些内容。

add return flag2; 添加return flag2; (or true or false) at the end of the method to be sure you have a return value even if choice is something other than 'F'. (在方法末尾)(或true或false),以确保您拥有返回值,即使选择不是'F'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM