0

要件

  1. ハゲタカはV、フクロウはO、イーグルはE...
  2. for各バード ウォッチャーが収集したデータを入力するためのループ。
  3. forループ内にはdo ... while、1 人のバード ウォッチャーが収集したデータを入力して処理するためのループがあります。
  4. do ... whileループ内ではswitch、鳥の種類ごとに卵の数を計算するステートメントが使用されます。an を入力すると、何もしないデフォルトのオプションが使用されxます。
  5. 鳥の種類として X が入力されると、do ... whileループが終了します。
  6. 合計部分は、以下のコードに従って問題ありません

さて、私の問題は、スイッチケースを通り抜けることができないように見えることです。最初のウォッチャーの情報を入力するように求められます。入力すると、次のウォッチャーに移動することはありません。

与えられた入力データは

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;
}
4

2 に答える 2

1

各スイッチケースの後に休憩が必要です。また、単一のバードウォッチャーがいつ終了したかを知らせるために、ブール変数「done」が必要です。

 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
于 2013-03-31T20:04:19.440 に答える
0

私はプログラム全体を書き直しましたが、今は動作しますが、入力に気をつけてください: 入力のタイプのために、常にいくつかを与える必要char-intがあります。 .

したがって、入力は次のようになります。

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;
}
于 2013-03-31T21:10:14.457 に答える