C++ 分配开关盒c++;
要求C++ 分配开关盒c++;,c++,loops,switch-statement,C++,Loops,Switch Statement,要求 秃鹫是V,猫头鹰是O,老鹰是E 一个for循环,用于输入每个鸟类观察者收集的数据 在for循环中,一个do。。。而循环输入和处理一名鸟类观察者收集的数据 在do中。。。而循环则使用开关语句来计算每种鸟的蛋数。当输入x时,将使用不执行任何操作的默认选项 do。。。当为鸟的类型输入X时,退出循环 根据下面的代码,总计部分正常 好的,现在我的问题是我好像无法通过我的开关箱。它会提示我输入第一个观察者的信息,当我输入它时,它不会移动到下一个观察者 给出的输入数据是 3 E2 O1 V2 E1 O3
for
循环,用于输入每个鸟类观察者收集的数据for
循环中,一个do。。。而
循环输入和处理一名鸟类观察者收集的数据do中。。。而
循环则使用开关
语句来计算每种鸟的蛋数。当输入x
时,将使用不执行任何操作的默认选项do。。。当为鸟的类型输入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;
}
#包括
#包括
使用名称空间std;
int main()
{
int totNrVultureEggs,TotnrEggs,TotnrOwloEggs,nrEggs,
nrVultureEggs,nrOwlEggs,nrBirdWatchers,nReggestered;
炭鸟;
//初始化每种鸟蛋数量的总计
观鸟者;
//环游观鸟者人数
对于(int i=0;i cout在每个切换案例之后,您都需要一个中断。此外,您还需要一个布尔变量“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
我重写了整个程序,现在它可以工作了,但要注意输入:
由于输入的类型,您必须始终提供两个字符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;
}
#包括
#包括
使用名称空间std;
int main(){
布尔多;
字符鸟型;
鹰蛋、猫头鹰蛋、秃鹫蛋;
int totEagleEggs、Totowlegs、Totvultureegs;
内特艾格斯特普、艾格辛、观鸟者;
观鸟者;
totEagleEggs=Totowlegs=totVultureEggs=0;
对于(int i=0;i 您是否忘记了break;
在每个案例中:
语句。这意味着只要匹配了任何案例,就会执行该点以下其余案例中的所有代码。我添加了它,但它仍然不想工作…案例“E”:案例“E”:nrEagleEggs=nrEagleEggs+nrEggs;break;case'O':case'O':nRowlegs=nRowlegs+nrEggs;break;case'V':case'V':case'V':nRvultureegs=nRvultureegs+nrEggs;break;默认值:nrBirdWatchers++;break;那又怎样你看到输出了吗?[LINK],然后程序停止响应,不再允许进一步的输入。我添加了它,但仍然没有给出所需的结果,直到它执行完全相同的[LINK]positmg.org/image/660ci1kv7是这样的输入E2O1 V2 E1 O3 X0 V2 V1 O1 E3 O2 E1 X0 V2 E1 X或类似E2 O1 V2 E1 X…在第二种情况下,您的程序应该可以工作。当您执行cin>>bird>>nFlags时,它希望bird和nFlags是空间分隔的,在您的情况下这是不正确的。您可能需要在该c中执行复杂的解析机制ase.这解决了我的开关盒问题…非常感谢…,但似乎无法在程序运行后显示我的最终结果对不起,我忘记了程序结束时的暂停[我从cmd启动命令提示符程序,而不是双击=P]。实际上,程序printa也会打印最终结果,但它会在您读取结果之前退出。您只需在命令返回0;
之前添加系统(“暂停”);
[我编辑了答案]好的,我已经添加了暂停,但由于某些原因,它不会像总数应该显示的那样超出for循环的末尾。我使用代码块并直接从代码块编译,不知道这有什么不同??不管怎样,工作很好,使用x0而不是在最后一个人使用x,然后打印总数,一切都是正确的好的,非常感谢你在英格罗索德给我带来的麻烦