[英]Matching user inputs and calculating the inputs by the number c++
我的问题是记录用户5个输入并将它们匹配到正确的代码。 singleperson
代码为1, couple
代码为2, family
代码为3。因此,用户输入的只是这3个数字。最后,我必须按类别计算总人数。 我运行了代码,并遇到运行时错误,说变量“组”周围的堆栈已损坏,夫妇和家庭均为0时,总是只有5个单打。对不起,我刚刚上学,我有点模糊。
int singleperson=0;
int couple=0;
int family3=0;
int groups[]={0,0,0,0,0};
cout << "Enter group #1:";
cin >>groups[0];
cout << "Enter group #2:";
cin >>groups[1];
cout << "Enter group #3:";
cin >>groups[2];
cout << "Enter group #4:";
cin >>groups[3];
cout << "Enter group #5:";
cin >>groups[4];
for (int a=0; a<=4;a++)
{
if(groups[a]=1)
{
singleperson= singleperson + 1;
}
else if(groups[a]=2)
{
couple = couple +1;
}
else
{
family3= family3+1;
}
}
cout<<"Statistics"<<endl;
cout <<singleperson<<"Singles"<<endl;
cout<<couple<<"Couples"<<endl;
cout<<family3<<"Families"<<endl;
因为group
大小为4
并且您尝试插入5th
位。 正如@ antonio-garrido指出的,检查相等性,我们使用==
并且=
用于赋值。
int groups[]={0,0,0,0,0};
for (int a=0 ; a <= 4; a++) {
if(groups[a] == 1) {
singleperson= singleperson + 1;
} else if(groups[a] == 2) {
couple = couple +1;
} else {
family3= family3+1;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.