我一直有一个问题,得到的if语句正常工作的代码。C++ while语句,嵌套if else
我有我需要它的一切,只是我们应该有多个条目输入,它只是自动使用响应和else语句不起作用。
int main()
{
string dep = "Deposit";
string with = "Withdrawl";
string bal = "Balance";
char choice;
cout << "PLease enter options A, B, C, or Q to quit.\n";
cin >> choice;
switch (choice) //to make them all the same, same as using toUpper
{
case 'a':
case 'A':
cout << "";
break;
case 'b':
case 'B':
cout << "";
break;
case 'q':
case 'Q':
cout << "";
break;
}
int count = 1;
while (count <= 4)
{
if (choice == 'a' || 'A')
cout << dep << endl;
else if (choice == 'b' || 'B')
cout << with << endl;
else if(choice == 'c' || 'C')
cout << bal << endl;
else
(choice !='a' && choice !='b' && choice !='c');
cout << "that is invalid, PLease enter options A, B, C, or Q to quit.\n";
++count ;
}
system ("PAUSE");
return 0;
}
'if(choice =='a'||'A')'不符合您的想法。您需要'if(choice =='a'|| choice =='A')'等。注意:您也可以将所有输入转换为大写或小写以减少测试。 –
另外,'else(choice!='a'&& choice!='b'&& choice!='c');'缺少'if',并且不应该有';'打算在那里完成。 – jogojapan
最后,您不会在循环中读取新的用户输入。您只需循环四次原始输入字符即可。 – jogojapan