Structures Printing
-
The following program is taking the inputs in the structures but when it comes to print them the program terminates abruptly. //OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers, //model, car brand and to store them in structures #include<stdio.h> #include<conio.h> #include<string.h> struct data{ char oname[20]; int chno; int model; char brand[20]; }; main() { int slot,i=0; typedef struct data a; printf("Enter the no. of data slots you want to make\n"); scanf("%d",&slot); a c[slot]; for (i=0;i<slot;i++) { printf("Enter the Owner's Name\n"); //gets(c[i].oname); scanf("%s",&c[i].oname[20]); printf("Enter the chassis number\n"); scanf("%d",&(c[i].chno)); printf("Enter the model year\n"); scanf("%d",&(c[i].model)); printf("Enter the brand name\n"); //gets(c[i].brand); scanf("%s",&c[i].brand[20]); } for (i=0;i<slot;i++) { printf("%s %d %d %s\n",c[i].oname[20], c[i].chno,c[i].model,c[i].brand[20]); } getche(); }
-
The following program is taking the inputs in the structures but when it comes to print them the program terminates abruptly. //OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers, //model, car brand and to store them in structures #include<stdio.h> #include<conio.h> #include<string.h> struct data{ char oname[20]; int chno; int model; char brand[20]; }; main() { int slot,i=0; typedef struct data a; printf("Enter the no. of data slots you want to make\n"); scanf("%d",&slot); a c[slot]; for (i=0;i<slot;i++) { printf("Enter the Owner's Name\n"); //gets(c[i].oname); scanf("%s",&c[i].oname[20]); printf("Enter the chassis number\n"); scanf("%d",&(c[i].chno)); printf("Enter the model year\n"); scanf("%d",&(c[i].model)); printf("Enter the brand name\n"); //gets(c[i].brand); scanf("%s",&c[i].brand[20]); } for (i=0;i<slot;i++) { printf("%s %d %d %s\n",c[i].oname[20], c[i].chno,c[i].model,c[i].brand[20]); } getche(); }
-
The following program is taking the inputs in the structures but when it comes to print them the program terminates abruptly. //OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers, //model, car brand and to store them in structures #include<stdio.h> #include<conio.h> #include<string.h> struct data{ char oname[20]; int chno; int model; char brand[20]; }; main() { int slot,i=0; typedef struct data a; printf("Enter the no. of data slots you want to make\n"); scanf("%d",&slot); a c[slot]; for (i=0;i<slot;i++) { printf("Enter the Owner's Name\n"); //gets(c[i].oname); scanf("%s",&c[i].oname[20]); printf("Enter the chassis number\n"); scanf("%d",&(c[i].chno)); printf("Enter the model year\n"); scanf("%d",&(c[i].model)); printf("Enter the brand name\n"); //gets(c[i].brand); scanf("%s",&c[i].brand[20]); } for (i=0;i<slot;i++) { printf("%s %d %d %s\n",c[i].oname[20], c[i].chno,c[i].model,c[i].brand[20]); } getche(); }
As Richard said, the program shouldn't compile at all. Also you need to put a
fflush(stdin)
call after eachscanf
statement so that the remaining characters in the input stream which cannot be read byscanf
are removed.«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) -
The following program is taking the inputs in the structures but when it comes to print them the program terminates abruptly. //OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers, //model, car brand and to store them in structures #include<stdio.h> #include<conio.h> #include<string.h> struct data{ char oname[20]; int chno; int model; char brand[20]; }; main() { int slot,i=0; typedef struct data a; printf("Enter the no. of data slots you want to make\n"); scanf("%d",&slot); a c[slot]; for (i=0;i<slot;i++) { printf("Enter the Owner's Name\n"); //gets(c[i].oname); scanf("%s",&c[i].oname[20]); printf("Enter the chassis number\n"); scanf("%d",&(c[i].chno)); printf("Enter the model year\n"); scanf("%d",&(c[i].model)); printf("Enter the brand name\n"); //gets(c[i].brand); scanf("%s",&c[i].brand[20]); } for (i=0;i<slot;i++) { printf("%s %d %d %s\n",c[i].oname[20], c[i].chno,c[i].model,c[i].brand[20]); } getche(); }
Hi, With reference to Mr Richards reply i made some changes which will make ur program to work. The changes are commented. the slot size should be created dynamically as suggested. Hope u can understand the code. I am beginner too. Good luck //OBJECT: To make a program to take the input from user as the names of the owner, chassis numbers, //model, car brand and to store them in structures #include<stdio.h> #include<conio.h> #include<string.h> struct data{ char oname[20]; int chno; int model; char brand[20]; }; const int slot =2; //changed here as pointed out by Mr Richard void main() { int i=0; typedef struct data a; printf("Enter the no. of data slots you want to make\n"); //scanf("%d",&slot); //commented here a c[slot]; for (i=0;i<slot;i++) { printf("Enter the Owner's Name\n"); //gets(c[i].oname); scanf("%s",c[i].oname); //&c[i].oname[20] not necessary printf("Enter the chassis number\n"); scanf("%d",&(c[i].chno)); printf("Enter the model year\n"); scanf("%d",&(c[i].model)); printf("Enter the brand name\n"); //gets(c[i].brand); scanf("%s",c[i].brand); //&c[i].brand[20] not necessary } for (i=0;i<slot;i++) { printf("%s %d %d %s\n",c[i].oname, c[i].chno,c[i].model,c[i].brand); } getche(); }