2015-10-10 121 views
-5

我无法找到do-while上的错误。如果我回答'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); 

需要帮助的人。我需要明天将这件事寄给我的老师。我对这种语言:(

+2

这看起来像C/C++,不C#。 –

+1

“for”的用途是什么?你只有一个循环,所以你可以自己追踪''i''变量,而不需要''for''来启动你。 – SWeko

+3

scanf()调用破坏堆栈,任何事情都是可能的。 yn变量是%c,而不是%s –

回答

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); 
} 
+1

答案会因提供更多解释而受益。 – Onik

+0

我添加了一些评论 – milevyo

1

两个主要错误是一个小白: 最关键的是:

float measure[i]; 

你必须准确申报表如大小:

float measure[50]; 

第二个是:scanf字符串格式为char "%c"不是"%s"

还存在其他错误