为什么我的代码会跳过此循环?

时间:2016-04-02 03:47:23

标签: c++

我提前为我的菜鸟问题道歉。我的第一次编程经验是我目前的课程。关于这个问题,

为什么我的代码跳过第二个while循环。当我为输入选择p或P时,我仍然会被要求输入与第一个while循环有关的输入,而不是第二个。 E.g分钟而不是白天/分钟。

 #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;
    }

5 个答案:

答案 0 :(得分:2)

正如@Vaughn Cato所说,你的代码中有一个return 0;,它不是条件语句的一部分。我假设您可能需要将return语句插入到一个if / else语句中。

我最好选择使用break;语句而不是return 0;来退出第一个循环。

答案 1 :(得分:1)

您的病情在逻辑上是不正确的。 您需要将其更改为:

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,你总是进入第一个循环 第二个循环也有同样的问题。

答案 2 :(得分:0)

因为您的第一个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' )

答案 3 :(得分:0)

因为每次只执行一次,所以最好使用

if (service == 'r' || service == 'R')

而不是第一次,而

if (service == 'p' || service == 'P')

而不是第二次。

while循环的想法是执行一个句子块零次或多次。

答案 4 :(得分:0)

无需使用while循环,因为代码只会运行一次,而service的值在循环内部不会发生变化。
如前所述已经解释过使用service == 'r' || service == 'R'
我们建议您使用if elseif代替while

if(service == 'r' || service == 'R')
{
   // your code
}
else if(service == 'p' || service == 'P')
{
    //you code
}

在这两个条件中删除return 0; 如果要多次运行条件,请使用循环。

如果您想多次运行它,请继续使用while循环,只需在循环内删除return 0;