我无法在do-while上找到错误。如果我回答N' N'问题。(该应用程序用于查找不确定数量的人的平均身高)。
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main(int argc, char** argv) {
int i;
float measure[i],sum,average;
char sex;
char yn;
do{
for(i=1;i;i++) {
printf("Persoa %d",i);
printf("\nIndique se é home (H) ou muller (M): ");
scanf("%s",&sex);
while((sex != 'M') && (sex != 'H'))
{
printf("Lo ha escrito mal.");
printf("\nIndique se é home (H) ou muller (M): ");
scanf("%s",&sex);
}
printf("Indique a súa measure (en metros): ");
scanf("%f",&measure[i]);
sum=sum+measure[i];
printf("\nDesea seguir? (Y/N): ");
scanf("%s",&yn);
while((yn != 'Y') && (yn != 'N'))
{
printf("Lo ha escrito mal.");
printf("\nDesea seguir? (Y/N): ");
scanf("%s",&yn);
}
}
}while(yn == 'Y');
average=sum/i;
printf("\nA media de measures é: %f m.",average);
return (EXIT_SUCCESS);
需要帮助的人。我明天需要把它寄给我的老师。我是这种语言的菜鸟:(
答案 0 :(得分:1)
两个主要错误是: 最关键的是:
float measure[i];
你必须准确地声明表的大小,例如:
float measure[50];
第二个是:char的scanf字符串格式为"%c"
而非"%s"
还存在其他错误。
答案 1 :(得分:0)
尝试对您的计划进行此更正
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
#define NB_MESURES 50
int main(int argc, char** argv) {
int i=0;
float measure[NB_MESURES] /* NB_MESURES is predefined to be 50*/
,sum=0,average=0;
char sex=0;
char yn=0;
while(1){ /*endless loop (1)*/
printf("Persoa %d",i);
printf("\nIndique se é home (H) ou muller (M): ");
while(1) /*endless loop (level 2)*/
{
scanf(" %c",&sex);
if((sex == 'M') || (sex == 'H')) break; /*Upper case*/
if((sex == 'm') || (sex == 'h')) break; /*Lower case*/
/* ok exit loop (level 2)
else show the help message */
printf("Lo ha escrito mal.");
printf("\nIndique se é home (H) ou muller (M): ");
}
printf("Indique a súa measure (en metros): ");
scanf("%f",&measure[i]);
sum=sum+measure[i];
i++;
if(i==NB_MESURES){
/*
we can not add more than 50 elements
exiting loop (1)
*/
printf("\nMax Allowed entries is %d\n",NB_MESURES);
break;
}else{
printf("\nDesea seguir? (Y/N): ");
while(1) /* loop (level 2) */
{
scanf(" %c",&yn);
if((yn == 'Y') || (yn == 'N')) break;
if((yn == 'y') || (yn == 'n')) break;
/* if the input was different than ['Y','y','N','n']
show newt message else we break and use yn in the next check to exit loop (level 1)
*/
printf("Lo ha escrito mal.");
printf("\nDesea seguir? (Y/N): ");
}
if((yn == 'N') || (yn == 'n')) break; /*Exit level 1*/
}
}
average=sum/i;
printf("\nA media de measures é: %f m.\n",average);
return (EXIT_SUCCESS);
}