fork download
  1. #include <stdio.h>
  2. #define MAX 15
  3.  
  4. int main(void) {
  5. // your code goes here
  6. char name[MAX];
  7. char c;
  8. int choice=1;
  9. while(choice>0 && choice<3)
  10. {
  11. printf("Enter your choice: ");
  12. scanf("%d", &choice);
  13.  
  14. switch(choice)
  15. {
  16. case 1:
  17. while ((c = getchar()) == '\n');
  18. ungetc(c, stdin);
  19. printf("What is your name? ");
  20. fgets(name, MAX, stdin);
  21. if ((strlen(name) > 0) && (name[strlen(name) - 1] == '\n'))
  22. name[strlen(name) - 1] = '\0';
  23. printf("Hello %s. Nice to meet you.\n", name);
  24. break;
  25.  
  26.  
  27. case 2:
  28. printf("Your choice is case 2\n");
  29. break;
  30. }
  31. }
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 4304KB
stdin
2
1
Hello there
1
Are you good
3
stdout
Enter your choice: Your choice is case 2
Enter your choice: What is your name? Hello Hello there. Nice to meet you.
Enter your choice: What is your name? Hello Are you good. Nice to meet you.
Enter your choice: