c++ - 为什么无论我输入什么,我的 "while"循环都会执行?

标签 c++ loops while-loop

/*********************************************************
** Purpose: Asks the user for cable package they bought **
** and the amount of hrs they used and //tells them     **
** their monthly bill                                   **
**********************************************************/                                        




#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    //Defining variables
    double hours_over;  //Amount of hrs the user went over their monthly allottment
    double extra_pay;   //Extra bill amount for going over monthly hrs allotted
    double monthly_bill;  //Monthly bill the user will pay
    int hours;          // How many hours the user used during the month
    char package;       //The package the user chose

    //Getting the package the user bought
    cout << "Your monthly subscription bill is based on your package.";
    cout << "\n\nWhat package did you buy? Enter A, B or C: ";
    cin >> package;

    //Validating user input-must enter A, B or C
    while (package != 'A' || package != 'B' || package != 'C')
    {
      cout << "\nPlease enter A, B or C(capitalized).";
      cout << "\n\nWhat package did you buy?: ";
      cin >> package;
    }

    //Getting hours the user used during month
    cout << "How many hours did you use?: ";
    cin >> hours;

    //Validating user input-hrs cant exceed 744
    while (hours > 744)
    {
      cout << "I'm sorry but your monthly usage cannot exceed 744 hrs.";
      cout << "\nPlease enter another number.";
      cout << "How many hours did you use?: ";
      cin >> hours;
    }

    //Fixing decimal place of answers
    cout << setprecision(2) << fixed << showpoint << endl;

    //Switch statement-go to the package the user bought
    switch (package)
    {
       case 'A':
            if (hours > 10)
            {
                      hours_over=hours-10;
                      extra_pay=hours_over*(2.00);
                      monthly_bill=9.95+extra_pay;

                      cout << "Your monthly bill is: $" << monthly_bill << endl;
            }
            else
            {
                 cout << "Your monthly bill is: $9.95";
            }
            break;

       case 'B':
            if (hours > 20)
            {
                      hours_over=hours-20;
                      extra_pay=hours_over;
                      monthly_bill=14.95+extra_pay;

                      cout << "Your monthly bill is: $" << monthly_bill << endl;
            }
            else
            {
                 cout << "Your monthly bill is: $14.95";
            }
            break;

       case 'C':
            cout << "Your monthly bill is: $19.95";
            break;

       default:
            break;   
    }

cin.get();
return 0;
}

最佳答案

你对 A、B 或 C 的测试是错误的

 while (package != 'A' || package != 'B' || package != 'C')

应该是

 while (package != 'A' && package != 'B' && package != 'C')

关于c++ - 为什么无论我输入什么,我的 "while"循环都会执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5400191/

相关文章:

c++ - valgrind 使用 --trace-children=yes 得到的结果不正确

python - 将列表中的每 N 个元素写入多个文本文件

c# - 生成 0 到 9 之间的三个唯一数字列表的最佳方法

c++ - 满足中断条件后继续循环

c++ - 井字棋数组错误

c++ - 通过属性在 C++ 和 QML 之间共享数组或列表

C 读取文件并将数字保存在数组中

php - mysqli_fetch 循环不工作

c++ - 为什么我可以从派生类调用基模板类方法

r - Rmarkdown 中的循环 : How to make an in-text figure reference? 图标题?