要求
- 秃鹰是 V,猫头鹰是 O,鹰是 E...
for
输入每个鸟类观察者收集的数据的循环。- 在
for
循环内部,一个do ... while
循环输入和处理一位观鸟者收集的数据。 - 在
do ... while
循环内部,一个switch
语句用于计算每种鸟的蛋数量。输入an 时使用默认选项,它什么都不做x
。 do ... while
当为鸟的类型输入 X 时,循环退出。- 根据下面的代码,总计部分很好
好的,现在我的问题是我似乎无法通过我的开关盒。它提示我输入第一个观察者的信息,当我输入它时,它永远不会移动到下一个观察者。
给出的输入数据是
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;
}