Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Structures Printing

Structures Printing

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Razanust
    wrote on last edited by
    #1

    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(); }

    L _ K 3 Replies Last reply
    0
    • R Razanust

      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(); }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Razanust wrote:

      a c[slot];

      This will not work, you cannot allocate an array with an unknown size. I also don't know how you got this to work in the first place is it does not comile (as is to be expected).

      1 Reply Last reply
      0
      • R Razanust

        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(); }

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        As Richard said, the program shouldn't compile at all. Also you need to put a fflush(stdin) call after each scanf statement so that the remaining characters in the input stream which cannot be read by scanf are removed.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        1 Reply Last reply
        0
        • R Razanust

          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(); }

          K Offline
          K Offline
          kanduripavan
          wrote on last edited by
          #4

          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(); }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups