fork(2) download
  1. import std.conv;
  2. import std.stdio;
  3. import std.string;
  4.  
  5. struct Human {
  6. string name;
  7. ushort age;
  8. }
  9.  
  10. void print_human_list(Human[] human_list) {
  11.  
  12. foreach(human; human_list) {
  13. writeln(human.name);
  14. writeln(human.age);
  15. }
  16. writeln();
  17. }
  18.  
  19. void add_new_human(ref Human[] human_list) {
  20.  
  21. write("Name: ");
  22. string name = readln.strip;
  23.  
  24. write("Age: ");
  25. ushort age = readln.strip.to!ushort;
  26.  
  27. Human tmp_human = {name, age};
  28. human_list ~= tmp_human;
  29. }
  30.  
  31. void main() {
  32.  
  33. Human[] human_list;
  34.  
  35. for(;;) {
  36.  
  37. writeln("A)dd New Human.");
  38. writeln("P)rint Human List.");
  39. writeln("Q)uit.");
  40. write(": ");
  41.  
  42. char choice = readln.strip.to!char;
  43.  
  44. switch (choice) {
  45. case('A'):
  46. add_new_human(human_list);
  47. break;
  48. case('P'):
  49. print_human_list(human_list);
  50. break;
  51. case('Q'):
  52. return;
  53. default:
  54. continue;
  55. }
  56. }
  57. }
Success #stdin #stdout 0s 3804KB
stdin
A
Ben
65
A
Stive
33
P
A
Bob
78
P
Q
stdout
A)dd New Human.
P)rint Human List.
Q)uit.
: Name: Age: A)dd New Human.
P)rint Human List.
Q)uit.
: Name: Age: A)dd New Human.
P)rint Human List.
Q)uit.
: Ben
65
Stive
33

A)dd New Human.
P)rint Human List.
Q)uit.
: Name: Age: A)dd New Human.
P)rint Human List.
Q)uit.
: Ben
65
Stive
33
Bob
78

A)dd New Human.
P)rint Human List.
Q)uit.
: