names_st.c 870 B

123456789101112131415161718192021222324252627282930313233343536
  1. // names_st.c -- define names_st functions
  2. #include <stdio.h>
  3. #include "names_st.h" // include the header file
  4. // function definitions
  5. void get_names(names * pn)
  6. {
  7. printf("Please enter your first name: ");
  8. s_gets(pn->first, SLEN);
  9. printf("Please enter your last name: ");
  10. s_gets(pn->last, SLEN);
  11. }
  12. void show_names(const names * pn)
  13. {
  14. printf("%s %s", pn->first, pn->last);
  15. }
  16. char * s_gets(char * st, int n)
  17. {
  18. char * ret_val;
  19. char * find;
  20. ret_val = fgets(st, n, stdin);
  21. if (ret_val)
  22. {
  23. find = strchr(st, '\n'); // look for newline
  24. if (find) // if the address is not NULL,
  25. *find = '\0'; // place a null character there
  26. else
  27. while (getchar() != '\n')
  28. continue; // dispose of rest of line
  29. }
  30. return ret_val;
  31. }