loop for scanf elements of struct | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

loop for scanf elements of struct

#include <stdio.h> #include <stdlib.h> struct emp{ char Nom[30],Prenom[30]; int Age; int salaire; }; void maxemp(struct emp p[], int n){ int i,max_a,max_s,pos,pos2; max_a=p[0].Age;max_s=p[0].salaire; for(i=0;i<n;i++){ if(p[i].Age>max_a){ max_a = p[i].Age; pos = i; } if(p[i].salaire>max_s){ max_s=p[i].salaire; pos2=i; } } printf("l'empoloye le plus age est %s %s\n", p[pos].Nom, p[pos].Prenom); printf("l'empoloye qui touche le plus grand salaire est %s %s\n", p[pos2].Nom, p[pos2].Prenom); } void minemp(struct emp p[], int n){ int i,min_a,min_s,pos,pos2; min_a=p[0].Age;min_s=p[0].salaire; for(i=0;i<n;i++){ if(p[i].Age<min_a){ min_a = p[i].Age; pos = i; } if(p[i].salaire<min_s){ min_s=p[i].salaire; pos2=i; } } printf("l'empoloye le plus jeune est %s %s\n", p[pos].Nom, p[pos].Prenom); printf("l'empoloye qui touche le moins est %s %s\n", p[pos2].Nom, p[pos2].Prenom); } int main(){ int n,i; struct emp p[n]; printf("entrer le nombre des employes : ");scanf("%d", &n); for(i=0;i<n;i++){ printf("entrer le nom de l'employe %d : ", i+1);scanf("%s", p[i].Nom); printf("entrer le prenom de l'employe %d : ", i+1);scanf("%s", p[i].Prenom); printf("entrer l'age de l'employe %d : ", i+1);scanf("%d", &p[i].Age); printf("entrer le salaire de l'employe %d : ", i+1);scanf("%d", &p[i].salaire); } maxemp(p, n); minemp(p, n); system("pause"); return 0; } when I enter the age of the first employee, the program stands, I don't know what is the cause or what is the problem ???

24th Jan 2020, 3:21 PM
Youssef Elouasbi
Youssef Elouasbi - avatar
1 Answer
+ 2
Use fflush(stdin) after loops beginning and end.. when standard input deal with lots of different data type it failed to respond so clear standard input buffer is the best way.. Follow this tutorial 👇 https://www.tutorialspoint.com/clearing-input-buffer-in-c-cplusplus
25th Jan 2020, 3:20 AM
Scooby
Scooby - avatar