gpt4 book ai didi

c - 我的程序中收到一些未报告的值

转载 作者:行者123 更新时间:2023-11-30 16:37:00 25 4
gpt4 key购买 nike

我的程序是要求用户输入其组中人员的姓名和年龄。在程序开始时用户并不知道人数。用户继续输入数据直到用户输入年龄。程序最终打印出平均年龄。

源码如下

#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;

}

当我编译时,它没有显示任何错误。当我运行时,它显示 exe 文件停止工作。

最佳答案

以下内容正在运行

必须更改 name[]scanf() 中的格式 ( http://www.cplusplus.com/reference/cstdio/scanf/ )

,必须移动 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;

}

关于c - 我的程序中收到一些未报告的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48155817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com