gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:52:41 28 4
gpt4 key购买 nike

/*********************************************************
** 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com