简体   繁体   English

我在程序中得到一些未报告的值

[英]i am getting some unreported values in my program

My program is to ask the user to enter the name and age of the person in his group. 我的程序是要求用户输入组中该人的姓名和年龄。 The number of person is not known to the user in the beginning of the program. 在程序开始时,用户不知道人数。 The user keeps on entering the data till the user enters the age zero . 用户继续输入数据,直到输入年龄为止。 The program finally prints the average age. 该程序最终将打印平均年龄。

The source code is below 源代码如下

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct group {
    char user_Name[21];
    int user_age;
}Group;
int main()
{
    Group  *REC1;
    FILE *out_file, *read_file;
    int count_age=0, age, sum_age, i;
    float avg_age;
    char name[21]="", str[21]= "details.txt";
    char sample_chr;
    //opening a file in writing mode
      out_file = fopen(str, "a");

    // test for files not existing. 
       if (out_file == NULL) 
       {   
           printf("Error! Could not open file\n"); 
           exit(-1); // must include stdlib.h 
       }

       printf("\nEnter the Details of the person:\n\n");
       do
       {
         printf("Enter the User Name:\n");
         fflush(stdin);
         scanf("%[^\n]",name);

         printf("Enter the Age:\n");
         fflush(stdin);
         scanf("%d",&age);


         if(age == 0)
         {
            break;

         }
         else
         {
            // write to file 
            fprintf(out_file,"%s,%d\n", name, age);
         }

       }while(1);

       read_file = fopen(str,"r");
            //counting the number of lines present in the above file
             sample_chr = getc(read_file);
             while (sample_chr != EOF)
             {
                if (sample_chr == '\n')
                   count_age = count_age +1;

               sample_chr = getc(read_file);
             }             
             rewind(read_file);

             //allocating space for array of structure dynamically
             printf("\n%d\n",count_age);
            count_age = count_age - 1;
             REC1  =  (Group*)malloc(count_age*sizeof(Group));

             //storing the values in array of structures
             for(i=0; i<=count_age; i++)
                fscanf(read_file, "%s,%d", REC1[i].user_Name, &REC1[i].user_age);


             fclose(read_file);
             fclose(out_file);
             for(i =0; i<=count_age; i++)
             printf("\n%s %d\n", REC1[i].user_Name, REC1[i].user_age);

             for(i=0, sum_age=0; i<=count_age; i++)
              sum_age = sum_age + REC1[i].user_age;

             avg_age =  (float)sum_age/(count_age);

            printf("\n\nThe average age is %f\n\n\n", avg_age); 



    system("pause");
    return 0;

}

while i am compiling it doesn't shows any error. 虽然我正在编译它不会显示任何错误。 When i am running it shows exe file stopped working. 当我运行时,它显示exe文件停止工作。

the following is working 以下工作

there had to be changed the format in scanf() of name[] ( http://www.cplusplus.com/reference/cstdio/scanf/ ) 必须更改name[] scanf()中的格式( http://www.cplusplus.com/reference/cstdio/scanf/

, the fclose(out_file) had to be moved and the ending condition of the while loop had to be changed ... ,必须移动fclose(out_file) ,并且必须更改while循环的结束条件...

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

typedef struct group {
    char user_Name[21];
    int user_age;
}Group;

int main()
{
    Group  *REC1;
    FILE *out_file, *read_file;
    int count_age=0, age, sum_age, i;
    float avg_age;
    char name[21]="", str[21]= "details.txt";
    char sample_chr;
    //opening a file in writing mode
      out_file = fopen(str, "w");

    // test for files not existing. 
       if (out_file == NULL) 
       {   
           printf("Error! Could not open file\n"); 
           exit(-1); // must include stdlib.h 
       }

       printf("\nEnter the Details of the person:\n\n");
       do
       {
         printf("Enter the User Name:\n");
         scanf("%21s[^\n]",name);

         printf("Enter the Age:\n");
         scanf("%d",&age);


         if(age == 0)
         {
            break;

         }
         else
         {
            // write to file 
            fprintf(out_file,"%s , %d \n", name, age);
         }

       }while(age != 0);

       fclose(out_file);

       read_file = fopen(str,"r");
            //counting the number of lines present in the above file
             sample_chr = getc(read_file);
             while (sample_chr != EOF)
             {
                if (sample_chr == '\n')
                   count_age = count_age +1;

               sample_chr = getc(read_file);
             }             
             rewind(read_file);

             //allocating space for array of structure dynamically
             printf("\n Number of group members : %d \n",count_age);
            count_age = count_age - 1;
             REC1  =  (Group*)malloc(count_age*sizeof(Group));

             //storing the values in array of structures
             for(i=0; i<=count_age; i++)
                fscanf(read_file, "%s , %d", REC1[i].user_Name, &REC1[i].user_age);


             fclose(read_file);

             for(i =0; i<=count_age; i++)
             printf("\n Name : %s   Age : %d \n", REC1[i].user_Name, REC1[i].user_age);

             for(i=0, sum_age=0; i<=count_age; i++)
              sum_age = sum_age + REC1[i].user_age;

             avg_age =  (float)sum_age/(count_age);

            printf("\n\nThe average age is %f\n\n\n", avg_age); 



       return 0;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM