0

要求

  1. 秃鹰是 V,猫头鹰是 O,鹰是 E...
  2. for输入每个鸟类观察者收集的数据的循环。
  3. for循环内部,一个do ... while循环输入和处理一位观鸟者收集的数据。
  4. do ... while循环内部,一个switch语句用于计算每种鸟的蛋数量。输入an 时使用默认选项,它什么都不做x
  5. do ... while当为鸟的类型输入 X 时,循环退出。
  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

在每个 switch case 之后你需要休息一下。此外,您需要一个布尔变量“完成”来告诉您单个观鸟者何时完成。

 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

我重写了整个程序,现在它可以工作了,但要注意输入:由于输入的类型,你必须给 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;
}
于 2013-03-31T21:10:14.457 回答