2012-02-09 171 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.

希望這有助於!