/*********************************************************
** 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;
}
答案 0 :(得分:3)
您对A,B或C的测试是错误的
while (package != 'A' || package != 'B' || package != 'C')
应该是
while (package != 'A' && package != 'B' && package != 'C')
答案 1 :(得分:2)
考虑你的表达:
while (package != 'A' || package != 'B' || package != 'C') {
让包的值为'A'。
评估为
false || true || true
这当然是真的。
答案 2 :(得分:1)
此行始终评估为true:
while (package != 'A' || package != 'B' || package != 'C')
应该是:
while (package != 'A' && package != 'B' && package != 'C')
答案 3 :(得分:0)
您应该检查cin是否能够流式传输所需类型的值ala if (cin >> my_int)
,然后在错误输入后使用std::cin.clear()
,然后再重新输入值。否则,垃圾值,例如说某些无法转换为int的文本输入会使std::cin
处于错误状态,并且甚至不会尝试下一个std::cin >> xxx
。
答案 4 :(得分:0)
“while”只会在您需要时循环播放,但“if”将始终触发;你是这个意思吗?关于A,B或C的“if”总是触发,因为你使用了“||”意思是“或”链接条件。对于你的变量的任何值,它总是正确的,它不是A,不是B,或者不是C!
答案 5 :(得分:0)
首先,这段代码:
//Validating user input-must enter A, B or C
if (package != 'A' || package != 'B' || package != 'C')
{
cout << "\nPlease enter A, B or C(capitalized).";
cout << "\n\nWhat package did you buy?: ";
cin >> package;
}
不起作用,因为(1)你将字符串(包)与字符进行比较,(2)你正在使用|| (或)代替&amp;&amp; (和)。另外(3)你可能想要“while”而不是“if”。
while循环对我来说很好。
答案 6 :(得分:0)
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;
}
如果在循环内输入package
值为 B ,该怎么办?它满足第一个条件package != 'A'
,因为它之后是 OR 操作( true || false || true 导致 true ),循环进入。您应该使用&&
代替。所以,改变
while (package != 'A' && package != 'B' && package != 'C')
{
// .....
}