Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5.  
  6. char *dobby= ""
  7. "   /     \\\n"
  8. "/- (*) |*)\\\n"
  9. "|/\\.  _>/\\|\n"
  10. "   \\__/    |\\\n"
  11. "   _| |_   \\-/\n"
  12. "  /|\\__|\\  //\n"
  13. " |/|   |\\//\n"
  14. " |||   | ~'\n"
  15. " ||| __|\n"
  16. " /_\\| ||\n"
  17. " \\_/| ||\n"
  18. "   |7 |7\n"
  19. "   || ||\n"
  20. "   || ||\n"
  21. "   /\\ \\ \\  \n"
  22. "  ^^^^ ^^^ \n";
  23.  
  24.  
  25. struct Wizard{
  26.         char name[12];
  27.         char house[12];
  28.         int age;
  29. };
  30.  
  31. void winner(){
  32.         uid_t euid;
  33.         euid = geteuid();
  34.         setresuid(euid,euid,euid);
  35.         system("cat flag");
  36. }
  37.  
  38. struct Hogwarts{
  39.         struct Wizard wizards[10];
  40.         unsigned int amount;
  41.         unsigned int max_wizards;
  42. };
  43.  
  44. void flushh(){
  45.         int c;
  46.         while((c = getchar()) != '\n' && c != EOF);
  47. }
  48.  
  49. void init_hogwarts(struct Hogwarts *hogwarts){
  50.         hogwarts->amount = 0;
  51.         hogwarts->max_wizards = 10;
  52. }
  53.  
  54. int add_wizard(struct Hogwarts *hogwarts){
  55.  
  56.         unsigned int index = hogwarts->amount;
  57.         if(index >= hogwarts->max_wizards){
  58.                 printf("Hogwarts is full right now, please try next year :(\n");
  59.                 return 0;
  60.         }
  61.  
  62.         printf("Enter wizard name: \n");
  63.         fgets(hogwarts->wizards[index].name, 12, stdin);
  64.  
  65.         printf("Enter wizard house: \n");
  66.         fgets(hogwarts->wizards[index].house, 12, stdin);
  67.  
  68.         printf("Enter wizard age: \n");
  69.         scanf("%d", &(hogwarts->wizards[index].age));
  70.         flushh();
  71.  
  72.         hogwarts->amount++;
  73.         return 1;
  74. }
  75.  
  76. void print_all_wizards(struct Hogwarts *hogwarts){
  77.  
  78.         int num = hogwarts->amount;
  79.         for(int i = 0; i < num; ++i){
  80.                 struct Wizard *wiz = &(hogwarts->wizards[i]);
  81.                 printf("---------------------------------------\n\n");
  82.                 printf("[%d] Name:  %s \n", i, wiz->name);
  83.                 printf("[%d] House: %s \n", i, wiz->house);
  84.                 printf("[%d] Age:   %d \n", i, wiz->age);
  85.                 printf("---------------------------------------\n\n");
  86.         }
  87. }
  88.  
  89. void modify_wizard(struct Hogwarts *hogwarts){
  90.         int index;
  91.         printf("What's the wizard index you want to modify?\n");
  92.         scanf("%d", &index);
  93.         flushh();
  94.  
  95.         printf("Enter wizard name: \n");
  96.         fgets(hogwarts->wizards[index].name, 12, stdin);
  97.  
  98.         printf("Enter wizard house: \n");
  99.         fgets(hogwarts->wizards[index].house, 12, stdin);
  100.  
  101.         printf("Enter wizard age: \n");
  102.         scanf("%d", &(hogwarts->wizards[index].age));
  103.         flushh();
  104. }
  105.  
  106.  
  107. int main(int argc, char **argv){
  108.  
  109.         struct Hogwarts hogwarts;
  110.         char action[2];
  111.  
  112.         init_hogwarts(&hogwarts);
  113.  
  114.         printf("Welcome to Hogwarts!\n\n");
  115.         printf("%s\n\n",dobby);
  116.         printf("Please choose an action:\n\n");
  117.  
  118.         while(1){
  119.                 printf("[0] Create a new wizard:\n");
  120.                 printf("[1] Modify an exisiting wizard:\n");
  121.                 printf("[2] Print all wizards\n\n");
  122.                 printf("[3] Exit\n\n");
  123.  
  124.                 fgets(action, 2, stdin);
  125.                 flushh();
  126.  
  127.                 switch(action[0]) {
  128.                         case '0':
  129.                                 add_wizard(&hogwarts);
  130.                                 break;
  131.                         case '1':
  132.                                 modify_wizard(&hogwarts);
  133.                                 break;
  134.                         case '2':
  135.                                 print_all_wizards(&hogwarts);
  136.                                 break;
  137.                         case '3':
  138.                                 exit(0);
  139.                                 break;
  140.                 }
  141.         }
  142.  
  143.         return 1;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement