fork download
  1. #include <stdio.h>
  2.  
  3. struct DB {
  4. char name[20];
  5. int age;
  6. char hometown[20];
  7. };
  8.  
  9. int main()
  10. {
  11. struct DB db[10];
  12. int i, n, count = 0;
  13.  
  14. while (1) {
  15. printf(" 1:create a database\n");
  16. printf(" 2:show the database\n");
  17. printf(" 3:exit\n");
  18. printf(" n=");
  19. scanf("%d", &n);
  20. printf("%d\n", n);
  21. switch (n) {
  22. case 1:
  23. printf(" Please input name, age and hometown\n");
  24. printf("db[%d].name=", count);
  25. scanf("%19s", db[count].name);
  26. printf("db[%d].age=", count);
  27. scanf("%d", &db[count].age);
  28. printf("db[%d].hometown=", count);
  29. scanf("%19s", db[count].hometown);
  30. count++;
  31. break;
  32. case 2:
  33. printf(" name age homwtown\n");
  34. for (i = 0; i < count; i++) {
  35. printf(" %s %d %s\n", db[i].name, db[i].age, db[i].hometown);
  36. }
  37. break;
  38. case 3:
  39. return 0;
  40. }
  41. }
  42. }
  43.  
Success #stdin #stdout 0.01s 1680KB
stdin
1
Tom
19
Tokyo
1
Alice
18
Osaka
2
3
stdout
 1:create a database
 2:show the database
 3:exit
 n=1
 Please input name, age and hometown
db[0].name=db[0].age=db[0].hometown= 1:create a database
 2:show the database
 3:exit
 n=1
 Please input name, age and hometown
db[1].name=db[1].age=db[1].hometown= 1:create a database
 2:show the database
 3:exit
 n=2
 name age homwtown
 Tom 19 Tokyo
 Alice 18 Osaka
 1:create a database
 2:show the database
 3:exit
 n=3