要求
for
循环,用于输入每个观鸟者收集的数据。for
循环内,do ... while
循环输入并处理由一名观鸟者收集的数据。do ... while
循环内,switch
语句用于计算每种类型鸟类的蛋数。输入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;
}
答案 0 :(得分: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
答案 1 :(得分:0)
我重写了整个程序,现在它可以工作了,但要注意输入:
因为输入的类型,你必须总是给几个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;
}