0

How to use loops in structure in C

#include<stdio.h> struct address { char nation[100]; char distrist[100]; int pin; }; int main(){ struct address person[2] ;//not working for(int i=0; i<2; i++){ printf("FOR PERSON %d",1); printf("\nEnter Naion :"); scanf("%s", person[1].nation); printf("\nEnter Dist :"); scanf("%s", person[1].distrist); printf("\nEnter PIN :"); scanf("%d", person[1].pin); } for (int 1-0; i<2; i++){ struct address person[1]; printf("\nADDRESS OF PERSON %d %s, %s, %d",1,person[1].nation, person[i].distrist, person[i].pin); } return 0;

13th Jan 2023, 9:22 AM
SD Anime
8 Respuestas
+ 4
Save code and share link here. That's help debugging is easy.. When you use fixed index for values, then why you need loop there.. ? It replaces values with inputs, every time in iteration so retains only last values. In 2nd, loop declaring again structure will lead errors. . And there is syntax error in loop: int 1-0 ? It must be like int i=0; And every time in loop, use index i, instead of 1 fixed index...
13th Jan 2023, 9:36 AM
Jayakrishna 🇮🇳
+ 2
#include<stdio.h>//ok struct address {//ok char nation[100]; //ok char distrist[100]; //ok int pin;//ok }; int main(){//ok struct address person[2];//ok for(int i=0; i<2; i++){//error printf("FOR PERSON %d",1); //error printf("\nEnter Naion :");//ok scanf("%s", person[1].nation); //error printf("\nEnter Dist :");//ok scanf("%s", person[1].distrist);//error printf("\nEnter PIN :"); //ok scanf("%d", person[1].pin);//error } for (int 1-0; i<2; i++){//error struct address person[1];//error printf("\nADDRESS OF PERSON %d %s, %s, %d",1,person[1].nation, person[i].distrist, person[i].pin);//error }//error return 0;//error A lots syntax errors.. Fix this errors.. learn the syntax first.
13th Jan 2023, 10:51 AM
Smith Welder
Smith Welder - avatar
+ 1
You can't use loop in struct, but you can use struct in loop. when there is not enough primitive data(int, double char, etc..), then you can create complex data such as a structure consisting of primitives, its the same like primitives, but complex. And use it like primitives in loops, conditions etc
13th Jan 2023, 2:59 PM
Smith Welder
Smith Welder - avatar
0
while Loop – While loop execute the code until condition is false. do – while loop. It also executes the code until condition is false. for Loop.
13th Jan 2023, 9:43 AM
william joe
william joe - avatar
0
That's why I want to which syntax should I use to use loop in structure?
13th Jan 2023, 2:31 PM
SD Anime
0
Can you give me a code example, it will really help me, I will greatly appreciate it?
13th Jan 2023, 4:04 PM
SD Anime
13th Jan 2023, 7:04 PM
Smith Welder
Smith Welder - avatar
0
#include<stdio.h> struct address { char nation[100]; char distrist[100]; int pin; }; int main(){ struct address person[2]; for(int i=0; i<2; i++){ printf("FOR PERSON %d", i+1); printf("\nEnter Nation :"); scanf("%s", person[i].nation); printf("\nEnter Dist :"); scanf("%s", person[i].distrist); printf("\nEnter PIN :"); scanf("%d", &person[i].pin); } for (int i=0; i<2; i++){ printf("\nADDRESS OF PERSON %d %s, %s, %d", i+1, person[i].nation, person[i].distrist, person[i].pin); } return 0; }
15th Jan 2023, 4:59 AM
Luthfi
Luthfi - avatar