为什么我的代码跳过了这个循环
Why is my code skipping this loop?
我为我的noob问题提前道歉。我第一次接触编程是在我目前的课程中。关于这个问题,
为什么我的代码跳过第二个while循环。当我选择p或p作为输入时,我仍然会被要求输入与第一个while循环有关的信息,而不是第二个。例如分钟数,而不是白天的分钟数/晚上的分钟数。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char service;
int number;
int minutes;
int dayMinutes;
int nightMinutes;
double bill;
double const REG_FEE = 10.00;
double const PREM_FEE = 25.00;
double const REG_MIN = 0.20;
double const PREM_DAY = 0.10;
double const PREM_NIGHT = 0.05;
cout << "Please enter your account number: ";
cin >> number;
cout << "Please enter your service type (regular or premium): ";
cin >> service;
while (service == 'r' || 'R')
{
cout << "How many minutes have been used for this service?: ";
cin >> minutes;
if (minutes < 50)
{
bill = REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
bill = ((minutes - 50) * REG_MIN) + REG_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << minutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
return 0;
}
while (service == 'p' || 'P')
{
cout << "How many minutes were used during the day?: ";
cin >> dayMinutes;
cout << "How many minutes were used during the night?: ";
cin >> nightMinutes;
if (dayMinutes > 75)
{
bill = ((dayMinutes - 75) * PREM_DAY) + PREM_FEE;
}
if (nightMinutes > 100)
{
bill = ((nightMinutes - 100) * PREM_NIGHT) + PREM_FEE;
bill = bill + PREM_FEE;
cout << fixed << showpoint << setprecision(2);
cout << "The account number entered was: " << number << "." << endl;
cout << "The service type entered was: " << service << "." << endl;
cout << "You used: " << dayMinutes + nightMinutes << " minutes." << endl;
cout << "Your bill is $" << bill << "." << endl;
}
else
{
cout << "You have entered an invalid service code.";
}
return 0;
}
return 0;
}
正如@Vaughn-Cato所说,您的代码中有一个return 0;
,它不是条件语句的一部分。我假设您可能需要将return语句插入其中一个if/else语句中。
我的最佳选择是使用break;
语句而不是return 0;
来只退出第一个循环。
您的条件在逻辑上不正确。您需要将其更改为:
while (service == 'r' || service == 'R')
另一个环路也是如此:
while (service == 'p' || service == 'P')
运算符==
的优先级高于运算符||
你写的是:
while (service == 'r' || 'R')
因此,在您的情况下,首先计算表达式service == 'r'
假设service
的值为'p'
,则表达式的求值结果为false
接下来,表达式false || 'R'
被求值,它总是true
,因为字符R
求值为true
。所以条件总是true
,并且你总是进入第一个循环
第二个循环也有同样的问题。
因为无论输入是什么,第一个while
的条件总是true
。
while (service == 'r' || 'R')
等同于:
while ( (service == 'r') || 'R') )
例如,输入'p'
while ( ('p' == 'r') || 'R') )
while ( (false || 'R') )
while ( (false || true) ) // 'R' 's equivalent decimal is greater
// than 0, so is always 'true'
while (true)
可能的解决方案:
while ( service == 'r' || service == 'R' )
无需使用while
循环,因为代码只运行一次,并且service
的值在循环内不会更改
如前所述,所有人都已解释使用service == 'r' || service == 'R'
我建议您使用if
elseif
而不是while
if(service == 'r' || service == 'R')
{
// your code
}
else if(service == 'p' || service == 'P')
{
//you code
}
在这两个条件下去除CCD_ 25
如果要多次运行一个条件,请使用循环。
如果你想多次运行它,那么继续使用while
循环,只需删除循环中的return 0;
。
由于每隔一段时间只执行一次,因此最好使用
if (service == 'r' || service == 'R')
代替第一时间和
if (service == 'p' || service == 'P')
而不是第二个while。
while循环的思想是将一组句子执行零次或多次。
- 使用 std::istream_iterator 时,它似乎跳过了空文件行 - 如果可能的话,如何避免这种情况?
- C++ For Loop在过程中被跳过了.如何解决?
- C++跳过了辛线
- 如何在我的代码中修复无穷循环?我认为它跳过了第二个CIN,因此它一直在循环
- QT Qtimeline跳过了第一个框架
- 为什么循环卡住了
- 输入后,我的程序似乎跳过了第一个IF语句,直接转到ELSE
- C RLE BMP压缩跳过了最后一个字节
- 提升单元测试似乎跳过了应执行的代码
- C 将数据读取到一个结构,但它跳过了我的一个获取线,使该元素空白
- 为什么我的程序跳过了一个步骤
- 当我采用测试用例数 = 1 时,我的程序终止.其他明智的是,它给出了正确的答案,但跳过了最后一个测试用例
- 为什么我的代码跳过了这个循环
- 请解释:在 for 循环之后跳过了行
- C++编译器似乎跳过了代码行
- 如何计算循环执行了多少次?C++
- 跳过了 int* 的琐碎分配
- C++ 为什么我的循环中断了
- GLSL交错似乎在3DTV上甚至跳过了行
- 为什么while循环被跳过了