我的程序是要求用户输入其组中人员的姓名和年龄。在程序开始时用户不知道人数。用户继续输入数据,直到用户输入年龄零。该程序最终打印平均年龄。
源代码在
之下#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文件停止工作。
答案 0 :(得分:0)
以下工作
必须更改 'This will make it so the screen doesn't "flicker" while the code is running
Application.ScreenUpdating = False
For Each cell In Range("B4:B16").Cells
If cell.Value = False Then
Rows(cell.Row & ":" & cell.Row).EntireRow.Hidden = True
End If
Next cell
'Turn screen updating back on or Excel will appear "frozen"
Application.ScreenUpdating = True
scanf()
(http://www.cplusplus.com/reference/cstdio/scanf/)
,必须移动name[]
并且必须更改while循环的结束条件......
fclose(out_file)