2013-03-31 83 views
0

要求分配開關殼體C++

  1. 雕是V,OWL是O,EAGLE爲E ...
  2. for循環到輸入數據中每個觀鳥已收集。
  3. for循環內,一個do ... while循環,用於輸入和處理由一個觀鳥者收集的數據。
  4. do ... while循環內有一個switch聲明用於計算每種鳥類的卵數。當輸入x時,將使用默認選項,該選項不執行任何操作。
  5. do ... while當爲某種類型的鳥輸入X時退出循環。
  6. 的共計部分是罰款,每個代碼如下

OK,現在我的問題是,我似乎無法通過我的開關的情況下得到的。它提示我輸入第一個觀察者信息,當我輸入它時,它永遠不會移動到下一個觀察者。

給出的輸入數據是

3 
E2 O1 V2 E1 O3 X0 
V2 V1 O1 E3 O2 E1 X0 
V2 E1 X 

這裏是我到目前爲止的代碼:您需要每個開關情況後休息

#include <iostream> 
#include <cstdlib> 

using namespace std; 

int main() 
{ 

int totNrVultureEggs, totNrEagleEggs, totNrOwlEggs, nrEggs, 
    nrVultureEggs, nrEagleEggs, nrOwlEggs, nrBirdWatchers, nrEggsEntered; 

char bird; 

// initialize grand totals for number of eggs for each type of bird 
cout << "How many bird watchers took part in the study?"; 
cin >> nrBirdWatchers; 

// loop over number of bird watchers 
for (int i = 0; i < nrBirdWatchers ;i++) 
{ 
// initialize totals for number of eggs for each type of bird 
// this bird watcher saw 
nrVultureEggs = 0; 
nrEagleEggs = 0; 
nrOwlEggs = 0; 
cout << "\nEnter data for bird watcher " << i + 1 << ":" << endl; 

//loop over bird watchers 
do{ 

    cin >> bird >> nrEggs; 
    switch (bird) 
    { 
     case 'E': 
     case 'e': 
      nrEagleEggs = nrEagleEggs + nrEggs; 

     case 'O': 
     case 'o': 
      nrOwlEggs = nrOwlEggs + nrEggs; 

     case 'V': 
     case 'v': 
      nrVultureEggs = nrVultureEggs + nrEggs; 

     default : 
     nrBirdWatchers++; 
     break; 

    } 

    }while (i < nrBirdWatchers) 
; 
cout << "Bird watcher " << i + 1 << " saw " << nrVultureEggs; 
cout << " vulture eggs, " << nrEagleEggs << " eagle eggs and "; 
cout << nrOwlEggs << " owl eggs " << endl; 
// increment grand totals for eggs 
} 

// display results 
cout << "\nTotal number of vulture eggs: " << totNrVultureEggs; 
cout << "\nTotal number of eagle eggs: " << totNrEagleEggs; 
cout << "\nTotal number of owl eggs: " << totNrOwlEggs; 
return 0; 
} 
+3

你需要一個'break' – nullpotent

+1

你忘了'打破;'在每個'情況:'聲明。這意味着只要ANY匹配,在該點以下的其他案例中的所有代碼都會被執行。 –

+0

我添加了它,但它仍然不想工作...案例'E': 案例'e': nrEagleEggs = nrEagleEggs + nrEggs; 休息; 案例'O': 案例'o': nrOwlEggs = nrOwlEggs + nrEggs; 休息; 'V': 'v': nrVultureEggs = nrVultureEggs + nrEggs; 休息; 默認值: nrBirdWatchers ++; 休息; – Peanut

回答

0

我重寫了整個節目,現在它的工作原理,但照顧輸入: 因爲輸入的類型,你必須給ALWAYS幾個char-int,或者你將有一個壞的時間xD [問題出在緩衝區]。

所以輸入將是:

3 
E2 O1 V2 E1 O3 X0 
V2 V1 O1 E3 O2 E1 X0 
V2 E1 X0 

以下來源:

#include <iostream> 
#include <cstdlib> 

using namespace std; 

int main(){ 

    bool done; 
    char birdType; 
    int eagleEggs, owlEggs, vultureEggs; 
    int totEagleEggs, totOwlEggs, totVultureEggs; 
    int eggsTemp, eggsIn, birdWatchers; 

    cout << "How many bird watchers took part in the study?"; 
    cin >> birdWatchers; 

    totEagleEggs = totOwlEggs = totVultureEggs = 0; 

    for (int i = 0; i < birdWatchers ;i++){ 
     eagleEggs = owlEggs = vultureEggs = 0; 
     done = false; 

     cout << endl; 
     cout << "Enter data for bird-watcher n. " << (i + 1) << ":" << endl; 
     do{ 
      cin >> birdType >> eggsTemp; 
      switch (birdType) 
      { 
       case 'E': 
       case 'e': 
       eagleEggs += eggsTemp; 
       totEagleEggs += eagleEggs; 
       break; 

      case 'O': 
       case 'o': 
       owlEggs += eggsTemp; 
       totOwlEggs += owlEggs; 
      break; 

      case 'V': 
      case 'v': 
       vultureEggs += eggsTemp; 
       totVultureEggs += vultureEggs; 
      break; 

      default: 
       done = true; 
      } 
     }while (!done); 

     cout << "The bird-watcher n. " << (i + 1) << " saw " << vultureEggs; 
     cout << " vulture eggs, " << eagleEggs << " eagle eggs and "; 
     cout << owlEggs << " owl eggs." << endl; 
    } 

    cout << endl; 
    cout << "Total number of vulture eggs: " << totVultureEggs << endl; 
    cout << "Total number of eagle eggs: " << totEagleEggs << endl; 
    cout << "Total number of owl eggs: " << totOwlEggs << endl; 
    system("PAUSE"); 
    return 0; 
} 
+0

這解決了我的問題,我的開關情況...謝謝你很......,但不能似乎得到我的最終結果顯示該計劃已循環 – Peanut

+0

對不起後,我忘了在節目的結尾處的停頓[我啓動命令提示符程序從CMD,不使用雙擊= P]。 其實程序打印也是最終的結果,但它會退出之前,你可以閱讀它們。 你剛纔添加'系統( 「暫停」);'命令之前'返回0;'。 [我編輯答案】 – ingroxd

+0

OK我已經加入了暫停,但由於某種原因,它不越過的結束FOR循環正如總數都應該顯示。 – Peanut

1

。此外,你需要一個布爾變量'done'來告訴你什麼時候一個birdwatcher完成。

bool done = false; //Flag to note when a birdwatcher is done 
do { 
    string data; 
    cin >> data; 
    bird = data[0]; 
    nrEggs = data[1]-0; 
    switch (bird) 
    { 
    case 'E': 
    case 'e': 
     nrEagleEggs = nrEagleEggs + nrEggs; 
     break; //was missing before 

    case 'O': 
    case 'o': 
     nrOwlEggs = nrOwlEggs + nrEggs; 
     break; //was missing before 

    case 'V': 
    case 'v': 
     nrVultureEggs = nrVultureEggs + nrEggs; 
     break; //was missing before 

    default : 
     done = true; //changed: No more birds to report 
     break; 

    } 
}while (!done) //Check if there are birds to report 
+0

我加了,但還是沒有給出想要的結果 – Peanut

+0

看看我編輯的評論。 – Tushar

+0

STILL它完全相同 – Peanut