/*********************************************************
** 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')
另外,用户仍然可以在第二次尝试时输入无效的软件包。两个输入都应该使用类似的while循环。 – Theran 2011-03-23 02:39:23
好的...这是正确的.....好吧,我需要把&&在那里...呃@ _ @ ..........即时通讯新的所有这 – nicholas000 2011-03-23 02:40:29
@Theran:看来代码被偷偷地改为在这里使用“while”而不是“if”(所以我的文章和你的评论没有太多意义)。改变我的帖子,以包括新的“while” – AndyG 2011-03-23 02:42:04
您应该检查cin是否能够传输所需类型的值ala if (cin >> my_int)
,然后在错误输入后使用std::cin.clear()
,然后再让它们重新输入该值。否则,像说一些文本输入不能转换为int的垃圾值会在错误状态下保留std::cin
,甚至不会尝试下一个std::cin >> xxx
。
考虑您的表达:
while (package != 'A' || package != 'B' || package != 'C') {
让包有值 'A' 。
这个计算结果为
false || true || true
这当然是正确的。
哦..... yur权利 – nicholas000 2011-03-23 02:39:56
“while”只会在你想要的时候循环,但“if”总是会触发;你是这个意思吗?关于A,B或C的“if”总是会因为使用“||”意思是“或”来链接条件。对于变量的任何值,总是如此,它不是A,或者不是B,或者不是C!
首先,此代码:
//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)你使用|| (或)而不是& &(和)。另外(3)你可能想要“而”而不是“如果”。
while循环对我来说工作得很好。
此行结果始终为true:
while (package != 'A' || package != 'B' || package != 'C')
这也许应该是:
while (package != 'A' && package != 'B' && package != '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;
}
如果该package
值是内环路进入乙。它满足第一个条件package != 'A'
并且因为它是一个或之后的操作(true || false || true导致true),循环进入。您应该改用&&
。所以,改变
while (package != 'A' && package != 'B' && package != 'C')
{
// .....
}
是啊,那是正确的....我现在明白了 – nicholas000 2011-03-23 02:49:32
- 1. 为什么我的while循环无论我输入什么都会触发?
- 2. 为什么我的while循环无限?
- 3. 为什么我输入0之后我的while循环中止?
- 4. 为什么我的while循环需要新的输入(C++)
- 5. 为什么我的while循环终止?
- 6. 为什么我的while循环以这种方式执行?
- 7. 为什么我while循环导致无限输出
- 8. 为什么我的while循环运行到无穷?
- 9. 无论你输入什么内容,为什么while循环都输出相同的内容?
- 10. 为什么我的while循环没有按照我的输入说多少次?
- 11. 为什么while循环只输入多个2的输入?
- 12. 为什么这个for循环进入无限循环执行?
- 13. 为什么这个while循环执行无限次?
- 14. 为什么我无法在UNIX while循环内正确读取用户输入?
- 15. 为什么我的while循环只循环一次
- 16. 为什么我的while循环在for循环内工作?
- 17. 为什么我的嵌套while循环无法正常工作
- 18. 为什么我的While循环无法正常工作?
- 19. 为什么我的while while循环永远不会结束? C++
- 20. 为什么while while循环会冻结我的页面?
- 21. 为什么while循环显示单行?
- 22. 为什么我的“While”循环继续进行?
- 23. 我的while循环进入无限循环,出了什么问题?
- 24. 为什么我似乎陷入循环?
- 25. 为什么我的程序不会重新输入第二个while循环? C++
- 26. 为什么while循环中的这条语句从未执行?
- 27. NodeJS:加密 - 无论输入什么,我为什么得到相同的散列?
- 28. 为什么不执行exec(“突破”)while循环
- 29. 为什么我在我的do while语句中遇到无限循环?
- 30. while循环做了什么?
你使用调试器吗?你有什么尝试?另外,如果您询问特定的代码片段,则没有理由复制/粘贴整个程序。当人们不得不盯着一大堆代码时,人们不太愿意提供帮助,而且没有更多的背景。 – 2011-03-23 02:32:38
即时通讯对不起....我的不好,它的第一回合,看看。这就是代码不断重复的地方 – nicholas000 2011-03-23 02:36:24
是的,我不应该粘贴整个代码 – nicholas000 2011-03-23 02:36:46