Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. ## util.h
  2. void printLines(int x);
  3.  
  4. ## util.c
  5. #include <stdio.h>
  6. #include "util.h"
  7.  
  8. void printLines(int x){
  9. for (int i=0; i<x; i++) {
  10. printf("Line %d\n", i + 1);
  11. }
  12. }
  13.  
  14.  
  15. ## main.c
  16. #include "util.h"
  17. #include <stdio.h>
  18. #define SENTINEL -999
  19.  
  20. // Declare a structure called Person with 3 members:
  21. // name, telNumber, age
  22. struct Person {
  23. char name[20];
  24. char telNumber[11];
  25. int age;
  26. }User;
  27.  
  28. // Prototype for function displayPerson
  29. void displayPerson(struct Person c);
  30.  
  31. int main (int argc, const char * argv[]) {
  32.  
  33. // Declare integer
  34. int nLines = 0, nEntry = 0;
  35. printf("Please enter the number of lines you require: \n");
  36.  
  37. // Request integer from user
  38. fflush(stdin);
  39. scanf("%d", &nLines);
  40.  
  41. // Check request is greater than zero
  42. if (nLines > 0) {
  43.  
  44. // If greater than 0 perform printLines with
  45. // requested number of lines as parameter
  46. printLines(nLines);
  47. }
  48.  
  49. printf("Entering do while loop. Please enter number of times to iterate\n Will print minimum of one time\n");
  50.  
  51. // Output to user and take input
  52. // Break loop once user enters SENTINEL (-999)
  53. do {
  54. printf("Integer) Continue -999) Stop \n");
  55. fflush(stdin);
  56. scanf("%d", &nEntry);
  57. } while (nEntry != SENTINEL);
  58.  
  59.  
  60. // Promt for name from user and assign to Person structure
  61. // User.name
  62. printf("Please enter your first name for array demonstration\n");
  63. fflush(stdin);
  64. scanf("%s", User.name);
  65.  
  66. // Display the 2nd letter of the name (array)
  67. printf("The 2nd letter of your name is: %c\n", User.name[1]);
  68.  
  69. // Promt for the age from the user and assign to Person structure
  70. // User.age requires address of operator as not an array
  71. printf("Please enter your age:\n");
  72. fflush(stdin);
  73. scanf("%d", &User.age);
  74.  
  75. // Prompt for the tel number and assign to Person structure
  76. // User.telNumber (string to keep leading zero)
  77. printf("Please enter your Telephone number:\n");
  78. fflush(stdin);
  79. scanf("%s", User.telNumber);
  80.  
  81. // Call displayPerson using User as parameter
  82. displayPerson(User);
  83.  
  84. return 0;
  85. }
  86.  
  87. // Function displayPerson declaration
  88. // Takes structure Person as parameter
  89. void displayPerson(struct Person c){
  90.  
  91. printf("Outputting information stored in structure\n");
  92.  
  93. // Output name member
  94. printf("Your name is: %s\n",c.name);
  95.  
  96. // Output age member
  97. printf("You are %d years old\n", c.age);
  98.  
  99. // Output telNumber member
  100. printf("Your telephone number is %s\n", c.telNumber);
  101. }
Add Comment
Please, Sign In to add comment