![](/img/trans.png)
[英]warning C4715: 'operator==' : not all control paths return a value
[英]warning C4715: 'bankingMenu' : not all control paths return a value (C++)
這是我的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: ;
}}
我該如何解決此錯誤?
首先,這是警告,而不是錯誤。 您的代碼將被編譯並運行,但是警告是編譯器說:“這可能不符合您的預期!您可能需要仔細檢查!”
它之所以說“並非所有控制路徑都返回值”,是因為您的函數並不總是返回任何值。 具體來說,如果您輸入“ F”,則僅返回任何內容。 要修復它,使您的函數始終返回某些內容。
添加return flag2;
(在方法末尾)(或true或false),以確保您擁有返回值,即使選擇不是'F'。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.