2012-02-09 172 views
5

我对C++编程的概念非常陌生。我希望有一个多条件,如果使用||语句或者在一个语句中和和/或&/&。当我问我的大学教授这件事。她说这是可能的,然后侮辱了我对这个问题的有限知识。我有权访问的所有示例都显示多条语句,只有一条显示||。它不显示它们一起使用。我会学习如何让线路工作。我会附上我的代码。问题领域是编码的最后一个位。多条件if语句C++

# include <iostream> 
# include <cstring> 

using namespace std; 

main() 
{ 

    const int maximumHours = 774; 
    char customerPackage; 
    double hoursUsed = 0, 
      packageA = 9.95, 
      packageB = 14.95, 
      packageC = 19.95, 
      overPackageA = 2.00, 
      overPackageB = 1.00, 
      overTime = 0, 
      amountDue = 0, 
      excessCharged = 0; 

    cout << "Please enter the customer's package: "; 
    cin >> customerPackage; 

    switch (customerPackage) 
    { 
     case 'a' : 
      cout << "Please enter the number of hours used: "; 
       cin >> hoursUsed; 
      break; 

     case 'A' : 
      cout << "Please enter the number of hours used: "; 
       cin >> hoursUsed; 
      break; 

     case 'b' : 
      cout << "Please enter the number of hours used: "; 
       cin >> hoursUsed; 
      break; 

     case 'B' : 
      cout << "Please enter the number of hours used: "; 
       cin >> hoursUsed; 
      break; 

     case 'c' : 
      cout << "Please enter the number of hours used: "; 
       cin >> hoursUsed; 
      break; 

     case 'C' : 
      cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
      break;   
     default: cout << "Error." 
      << " Please enter the customer's purchased package: "; 
     cin >> customerPackage; 
    }  

    if (customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10)   
     amountDue = packageA; 
     else 
      overTime = packageA - hoursUsed; 
      excessCharged = overTime * overPackageA; 
      amountDue = packageA + excessCharged; 
} 
+6

与'=='比较,而不是'='。并且一定要使用'()'来消除你的条件。否则......怎么了?将你的代码称为“问题区域”并不能让我们继续研究你希望它做什么,而不是那么做。 – 2012-02-09 16:11:54

+0

比较运算符在c/C++上为* == * – Gigi 2012-02-09 16:12:25

+3

另外,请自己帮忙,并在每个比较周围放置括号,以便您和其他人确信&&和||的顺序。以上。 – 2012-02-09 16:13:49

回答

10

您的问题是&&的优先级高于||所以你需要括号。正如评论指出的,你还需要使用==,而不是分配(=)的:

if ((customerPackage =='a' || customerPackage =='A') && hoursUsed >= 10)

0

您可以使用括号来指定这些布尔运算符的执行顺序。你可能想评估||第一,所以你会使用:

if ((customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10) 

&&通常由默认的第一个评价,因为它具有更高的优先级,让你的代码是相同的:

if (customerPackage == 'a' || (customerPackage == 'A' && hoursUsed >= 10)) 

另外,如评论中所述,使用==进行比较,=进行分配。

+0

谢谢。这是非常有用的信息,如果我需要再次寻求帮助,我会更加细致。 – user1200066 2012-02-09 16:54:47

+0

超出我的职位空间。将重新发布新信息 – user1200066 2012-02-09 18:52:22

4
if (customerPackage ='a' || customerPackage ='A' && hoursUsed >= 10) 

您是所以接近有正确的答案。让我给你两个提示:

  1. =运营商是不一样的==操作。 =是赋值运算符。它评估其右侧并将结果存储在其左侧命名的变量中。你想要==,等于运算符。它测试看它的右侧和左侧是否相等。

  2. 使用括号(...)来强制执行您的评估顺序意向。你明确的意思是说:“如果customerPackage是'a'或者是'A',并且小时数足够大,那么......”。

试试这个行:

if ((customerPackage == 'a' || customerPackage == 'A') && hoursUsed >= 10) 
6

其他人已经帮你与你已经注意到这个问题。

else 
     overTime = packageA - hoursUsed; 
     excessCharged = overTime * overPackageA; 
     amountDue = packageA + excessCharged; 

如果你想所有这三个由else控制的那些语句,你需要将它们用括号来创建一个复合语句:我跟你显然没有注意到(还)一个单独的问题开始:

else { 
    overTime = packagA - hoursUsed; 
    excessCharged = overTime * overPackageA; 
    amountDue = packageA + excessCharged; 
} 

因为它现在代表,你的代码是真的:

else 
     overTime = packageA - hoursUsed; 
    excessCharged = overTime * overPackageA; 
    amountDue = packageA + excessCharged; 

即对于excessCharged和的计算是否执行,而不管if声明中的条件是否为真或假。

我还注意到,您的switch声明并没有真正大有作为:

switch (customerPackage) 
{ 
    case 'a' : 
     cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
     break; 

    case 'A' : 
     cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
     break; 

    case 'b' : 
     cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
     break; 

    case 'B' : 
     cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
     break; 

    case 'c' : 
     cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
     break; 

    case 'C' : 
     cout << "Please enter the number of hours used: "; 
     cin >> hoursUsed; 
     break;   
    default: cout << "Error." 
     << " Please enter the customer's purchased package: "; 

特别是,你究竟需要为所有的情况下(除了默认)同样的动作。您可以通过使用落空的情况下有点简化这个:

switch (customerPackage) { 
    case 'a': 
    case 'A': 
    case 'b': 
    case 'B': 
    case 'c': 
    case 'C': 
      cout << "Please enter the number of hours used: "; 
      cin >> hoursUsed; 
      break; 
    default: 
     cout << "Error " /* ... */; 
} 

或者,你可能会考虑这样的:

static const char valid[] = "aAbBcC"; 

if (strchr(valid, userPackage)) { 
    cout << "Please enter the number of hours used: "; 
    cin >> hoursUsed; 
} 
else { 
    std::cout << "Error: Please enter the customer's purchased package"; 
    std::cin >> userPackage; 
} 

就我个人而言,我会不同结构的东西了一下:首先得到一个有效的输入,那么获得下:

do { 
    std::cout << "Please enter the customer's purchased package (a, b, or c): "; 
    std::cin >> userPackage; 
} while (!strchr(valid, userPackage)); 

std::cout << "Please enter the number of hours used: "; 
std::cin >> hoursUsed; 

if (tolower(customerPackage == 'a') && hoursUsed >= 10) 
// ... 
+0

首先感谢所有帮助我的人。我已经根据这里分享的信息做出了修改,并且在哪里提供了应有的信息。我几乎完成了这个程序。我有两个问题。一个问题是通过所有的软件包并计算错误的数量。我已经回去,并在需要的时候提出了关于不脱离if语句的问题的建议更改。我知道这是我缺少的一小步。再次感谢所有人。 – user1200066 2012-02-09 18:07:35

+0

+1进行详细分析。 – erict 2013-06-21 15:57:59

0

有了你有(你问other question)的新的问题,你需要一些调整。

if ((customerPackage == 'b' || customerPackage == 'B') && hoursUsed <= 20) 

    amountDue = packageB; 

    else 
    { 
     /* calculations */ 
    } 

是不正确的,这应该是

if (customerPackage == 'b' || customerPackage == 'B') 
{ 
    if (hoursUsed <= 20) 
    { 
     amountDue = packageB; 
    } 
    else 
    { 
     /* calculations */ 
    } 
} 

否则,当包= B和小时= 20,否则计算将在所有其他情况下完成的第一条语句只会被执行,像当包是A,或C.

希望这有助于!