我需要帮助这个小项目我做了写部分,但我不能理解读取部分时使用字符串值的scanf并搜索它和printf(显示)这样的输出,当我输入名称
Name: Ahsan Iqbal
Phone No.: bla bla bla
Address: bla bla bla
RESULT:
BEE: 90.00
CF&P: 90.00
Physics: 90.00
P.st: 90.00
C&AG: 90.00
Total Percentage: 90.00 ;
代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
int x=1;
FILE *f = fopen("Data.txt", "a+");
if (f == NULL)
{
FILE *f = fopen("Data.txt", "w");
}
do
{
printf("Please Select Options\n1.New Entry \n2.Search Entry \n3.Exit \nOption:");
scanf(" %d",&x);
if(x == 1) //new entry
{
float z[5];
char y,name[100],phone[20],address[256];
printf("Please Enter Name\n");
scanf(" %[^\n]",&name);
printf("Please Enter Phone Number\n");
scanf(" %[^\n]",&phone);
printf("Please Enter The Address\n");
scanf(" %[^\n]",&address);
printf("Do you want to save result (y/n)\n");
scanf(" %c",&y);
if (y == 'y' || y == 'Y')
{
printf("Please Enter BEE Marks\n");
scanf(" %f",&z[0]);
printf("Please Enter CF&P Marks\n");
scanf(" %f",&z[1]);
printf("Please Enter Physics Marks\n");
scanf(" %f",&z[2]);
printf("Please Enter P.st Marks\n");
scanf(" %f",&z[3]);
printf("Please Enter C&AG Marks\n");
scanf(" %f",&z[4]);
z[5] = ((z[0] +z[1] +z[2] +z[3] +z[4])/500)*100;
}
fprintf(f, "Name: %s \n",name );
fprintf(f, "Phone No.: %s\n",phone );
fprintf(f, "Address: %s\n",address );
if (y == 'y' || y == 'Y')
{
fprintf(f, "RESULT:\n");
fprintf(f, "BEE: %3.2f\n",z[0] );
fprintf(f, "CF&P: %3.2f\n",z[1] );
fprintf(f, "Physics: %3.2f\n",z[2] );
fprintf(f, "P.st: %3.2f\n",z[3] );
fprintf(f, "C&AG: %3.2f\n",z[4] );
fprintf(f, "Total Percentage: %3.2f ;\n",z[5] );
}
}
else if (x == 2) //Search
{
char name[100],temp[256];
printf("Please Enter First Name To Search\n");
scanf(" %[^\n]",&name); //need help after this
while(!feof(f)) {
fgets(name, 100, f);
printf(" %s", name);
}
}
printf("\n\n");
}while(x <= 2);
fclose(f);
}