if else语句循环或重复直到true

时间:2013-10-18 03:47:10

标签: c++ loops if-statement repeat

如果投标金额少于所需金额,我不知道如何循环或重复cin >> cashTendered

到目前为止,这就是我所拥有的。

if (cashTendered > || == total)
{
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";
}
else
{
cout << "You did not tender enough money to cover the total cost.\n"
<< "Please enter amount of cash tendered: $";
}

所以现在我想要if语句重复,直到为真。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

while(true)
{
   cin >> cashTendered
   if (cashTendered >= total)
   {
      cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
      << "Have a great day!\n\n\n\n\n";
      break;
   }
   else
   {
      cout << "You did not tender enough money to cover the total cost.\n"
      << "Please enter amount of cash tendered: $";
   }
}

答案 1 :(得分:0)

看看loops in c++。此外,如果条件可以缩短为>=而不是> || ==。此外,既然你想重复,直到cashTendered为&gt; = total,你需要一个循环来检查条件,如果cashTendered是&lt;总

while (cashTendered < total)
{
  cout << "You did not tender enough money to cover the total cost.\n"
  << "Please enter amount of cash tendered: $";
  cin >> cashTendered;
}
cout << "Your change is: $" << fixed << setprecision(2) << change << ".\n\n"
<< "Have a great day!\n\n\n\n\n";