+ 1

Why the part of findbyname it retuns me "does not exist" and also show me informations

#include<stdio.h> #include<string.h> #include<conio.h> #include<stdbool.h> int n,m; struct student{ long int Id; char name[10]; float score[4]; float math,physics,programming; float sum; float avr; }st[]; void storeinfo(){ printf("Please enter student number : "); scanf("%d",&n); for(int i=0;i<n;i++){ printf("\nThe student %d id ",i+1); scanf("%ld",&st[i].Id); getchar(); printf("The student name "); gets(st[i].name); printf("physics: "); scanf("%f", &st[i].physics); printf("math: "); scanf("%f", &st[i].math); printf("programming: "); scanf("%f", &st[i].programming); printf("\n\n"); } } void avr(){ float sum; float avr; for(int i=0;i<n;i++){ sum=st[i].programming + st[i].math + st[i].physics; avr=sum/3; st[i].avr=avr; printf("Average mark of %s is %.2f\n",st[i].name,st[i].avr); } } void grade(){ for(int i=0;i<n;i++){ if(st[i].avr>100 || st[i].avr<0)printf("Impossible"); else if(st[i].avr>79 && st[i].avr<=100)printf("Excellent work\n"); else if(st[i].avr>59 && st[i].avr<=80)printf("Good work\n"); else if(st[i].avr>49 && st[i].avr<60)printf("Succeed\n"); else printf("you need to work hard\n"); } } void findbyname() { bool j; char name[10]; printf("The name you looking for : "); getchar(); gets(name); for(int i=0;i<n;i++){ if(strcmp(st[i].name,name)==0){ printf("these are %s Informations : \n",st[i].name); printf("Id : %d",st[i].Id); printf("\nMath: %.2f",st[i].math); printf("\nPhysics: %.2f",st[i].physics); printf("\nProgramming: %.2f",st[i].programming); j=1; } if(j!=1){printf("\nDoes not exist");} } } void findbyId() { int j; printf("The Id of the student you looking for : "); scanf("%d",&j); getchar();

15th May 2022, 1:34 PM
Amine Hazmiri
Amine Hazmiri - avatar
3 Answers
+ 1
//part2 of the code void findbyId() { int j; printf("The Id of the student you looking for : "); scanf("%d",&j); getchar(); for(int i=0;i<n;i++){ if(st[i].Id ==j){ printf("These are student Informations with the Id : %d\n",st[i].Id); printf("\nName : %s\n",st[i].name); printf("Math: %.2f\n",st[i].math); printf("Physics: %.2f\n",st[i].physics); printf("Programming: %.2f\n\n",st[i].programming); } else if(st[i].Id !=j){ printf("Does not exist\n\n");} } } void display(){ for(int i=0;i<n;i++){ printf("Student Id : %d\n",st[i].Id); printf("Name : %s\n",st[i].name); printf("Math: %.2f\n",st[i].math); printf("Physics: %.2f\n",st[i].physics); printf("Programming: %.2f\n",st[i].programming); printf("Average mark of %s is %.2f\n",st[i].name,st[i].avr); printf("\n\n"); } } void displaybygrade(){ int p; int k=0; for(int i=0;i<n;i++){ if(st[i].avr>=50){ printf("Student Id : %d\n",st[i].Id); printf("Name : %s\n",st[i].name); k++; } } printf("the number of Students who succeed is : %d\n",k); printf("\n\n"); } void sup(){ int a; printf("the Id of the Student you want to remove : "); scanf("%d",&a); for(int i=0;i<n;i++){ if(a==st[i].Id){ for(int j=i;j<n;j++) st[j]=st[j+1]; n--; } }printf("the number of Students left is : %d\n",n); } void main(){ int choice; bool i; do{ printf("\n ============== Menu ===============\n"); printf("\t 1. Enter the student Informations\n"); printf("\t 2. Calculate the average\n"); printf("\t 3. Show the grade\n"); printf("\t 4. Search by student Name\n"); printf("\t 5. Search by student Id\n "); printf("\t 6. Display students informations\n"); printf("\t 7. Display number of student
15th May 2022, 1:35 PM
Amine Hazmiri
Amine Hazmiri - avatar
+ 1
//part3 printf("\t 8. Delete students informations using Id\n"); printf("\t 0. exit\n"); printf("\n\n"); printf("Please choose an option :"); scanf("%d",&choice); switch(choice){ case 1: storeinfo(); break; case 2: avr();break; case 3: grade();break; case 4: findbyname();break; case 5: findbyId();break; case 6: display();break; case 7: displaybygrade();break; case 8: sup();break; default: printf("Invalid Task");break; } printf("press(0) to exit"); scanf("%d",&i); }while(i==1); }
15th May 2022, 1:36 PM
Amine Hazmiri
Amine Hazmiri - avatar
0
Lets say you place have the names of Amine, Bob, Carlos, and David findByName() bool j; //not set, might be 0 or 1 
 //lets assume j = 0 this run //assuming user input Carlos for (int i = 0; i < n;i ++) //loop 1 (strcmp(Amine,Carlos) != 0) (0 != 1) print Does not Exist //loop 2 (strcmp(Bob,Carlos) != 0 ) (0 != 1)print Does not Exist //loop 3 (strcmp(Carlos,Carlos) == 0 ) print info j = 1 ( 1 == 1) // loop 4 ( strcmp(David,Carlos) != 0 ) (1 == 1 ) // end of for loop
15th May 2022, 2:08 PM
Raul Ramirez
Raul Ramirez - avatar