-1

cin >> cashTendered如果投标的金额小于要求的金额,我不确定如何循环或重复 a 。

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

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 语句重复直到为真。

有什么建议么?

4

2 回答 2

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: $";
   }
}
于 2013-10-18T04:02:16.103 回答
0

看看c++ 中的循环。此外,如果条件可以缩短为,>=而不是> || ==. 此外,由于您想重复直到 cashTendered >= total,因此您需要一个循环来检查 cashTendered 是否 < total 的条件。

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";
于 2013-10-18T04:49:23.170 回答