2011-03-23 80 views
0
/********************************************************* 
** 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; 
} 
+1

你使用调试器吗?你有什么尝试?另外,如果您询问特定的代码片段,则没有理由复制/粘贴整个程序。当人们不得不盯着一大堆代码时,人们不太愿意提供帮助,而且没有更多的背景。 – 2011-03-23 02:32:38

+0

即时通讯对不起....我的不好,它的第一回合,看看。这就是代码不断重复的地方 – nicholas000 2011-03-23 02:36:24

+0

是的,我不应该粘贴整个代码 – nicholas000 2011-03-23 02:36:46

回答

3

您的A,B或C的测试是错误的

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

应该

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

另外,用户仍然可以在第二次尝试时输入无效的软件包。两个输入都应该使用类似的while循环。 – Theran 2011-03-23 02:39:23

+0

好的...这是正确的.....好吧,我需要把&&在那里...呃@ _ @ ..........即时通讯新的所有这 – nicholas000 2011-03-23 02:40:29

+0

@Theran:看来代码被偷偷地改为在这里使用“while”而不是“if”(所以我的文章和你的评论没有太多意义)。改变我的帖子,以包括新的“while” – AndyG 2011-03-23 02:42:04

0

您应该检查cin是否能够传输所需类型的值ala if (cin >> my_int),然后在错误输入后使用std::cin.clear(),然后再让它们重新输入该值。否则,像说一些文本输入不能转换为int的垃圾值会在错误状态下保留std::cin,甚至不会尝试下一个std::cin >> xxx

2

考虑您的表达:

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

让包有值 'A' 。

这个计算结果为

false || true || true 

这当然是正确的。

+0

哦..... yur权利 – nicholas000 2011-03-23 02:39:56

0

“while”只会在你想要的时候循环,但“if”总是会触发;你是这个意思吗?关于A,B或C的“if”总是会因为使用“||”意思是“或”来链接条件。对于变量的任何值,总是如此,它不是A,或者不是B,或者不是C!

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)你使用|| (或)而不是& &(和)。另外(3)你可能想要“而”而不是“如果”。

while循环对我来说工作得很好。

1

此行结果始终为true:

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

这也许应该是:

while (package != 'A' && package != 'B' && package != 'C') 
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值是内环路进入。它满足第一个条件package != 'A'并且因为它是一个之后的操作(true || false || true导致true),循环进入。您应该改用&&。所以,改变

while (package != 'A' && package != 'B' && package != 'C') 
{ 
    // ..... 
} 
+0

是啊,那是正确的....我现在明白了 – nicholas000 2011-03-23 02:49:32

相关问题