要件
- ハゲタカはV、フクロウはO、イーグルはE...
for
各バード ウォッチャーが収集したデータを入力するためのループ。for
ループ内にはdo ... while
、1 人のバード ウォッチャーが収集したデータを入力して処理するためのループがあります。do ... while
ループ内ではswitch
、鳥の種類ごとに卵の数を計算するステートメントが使用されます。an を入力すると、何もしないデフォルトのオプションが使用されx
ます。- 鳥の種類として X が入力されると、
do ... while
ループが終了します。 - 合計部分は、以下のコードに従って問題ありません
さて、私の問題は、スイッチケースを通り抜けることができないように見えることです。最初のウォッチャーの情報を入力するように求められます。入力すると、次のウォッチャーに移動することはありません。
与えられた入力データは
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;
}