Advertisement
dsmithhayes

Structure Example

Apr 20th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.77 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct user{    //the structure is like a variable type
  4.         //like int, float, char, etc
  5.     char *name; //these are the elements of the structure
  6.     int id;
  7.     float score;
  8. };
  9.  
  10. struct user player[4];  //this is an array of 4 user structs called player
  11.  
  12. int main(void){
  13.     int i;
  14.  
  15.     for(i = 0; i < 4; i++){ //get all the information from the user
  16.         printf("Name: ");
  17.         gets(player[i].name);   //like the name
  18.         player[i].id++; //automatically increase the ID
  19.         printf("Score: ");
  20.         scanf("Score: %f", player[i].score);    //get an arbitrary score
  21.     }
  22.  
  23.     printf("ID\tName\tScore\n--\t----\t-----\n");
  24.     for(i = 0; i < 4; i++){ //print out the elements from the structs
  25.         printf("%d\t%s\t%.2f", player[i].id, player[i].name, player[i].score);
  26.     }
  27.  
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement