Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## util.h
- void printLines(int x);
- ## util.c
- #include <stdio.h>
- #include "util.h"
- void printLines(int x){
- for (int i=0; i<x; i++) {
- printf("Line %d\n", i + 1);
- }
- }
- ## main.c
- #include "util.h"
- #include <stdio.h>
- #define SENTINEL -999
- // Declare a structure called Person with 3 members:
- // name, telNumber, age
- struct Person {
- char name[20];
- char telNumber[11];
- int age;
- }User;
- // Prototype for function displayPerson
- void displayPerson(struct Person c);
- int main (int argc, const char * argv[]) {
- // Declare integer
- int nLines = 0, nEntry = 0;
- printf("Please enter the number of lines you require: \n");
- // Request integer from user
- fflush(stdin);
- scanf("%d", &nLines);
- // Check request is greater than zero
- if (nLines > 0) {
- // If greater than 0 perform printLines with
- // requested number of lines as parameter
- printLines(nLines);
- }
- printf("Entering do while loop. Please enter number of times to iterate\n Will print minimum of one time\n");
- // Output to user and take input
- // Break loop once user enters SENTINEL (-999)
- do {
- printf("Integer) Continue -999) Stop \n");
- fflush(stdin);
- scanf("%d", &nEntry);
- } while (nEntry != SENTINEL);
- // Promt for name from user and assign to Person structure
- // User.name
- printf("Please enter your first name for array demonstration\n");
- fflush(stdin);
- scanf("%s", User.name);
- // Display the 2nd letter of the name (array)
- printf("The 2nd letter of your name is: %c\n", User.name[1]);
- // Promt for the age from the user and assign to Person structure
- // User.age requires address of operator as not an array
- printf("Please enter your age:\n");
- fflush(stdin);
- scanf("%d", &User.age);
- // Prompt for the tel number and assign to Person structure
- // User.telNumber (string to keep leading zero)
- printf("Please enter your Telephone number:\n");
- fflush(stdin);
- scanf("%s", User.telNumber);
- // Call displayPerson using User as parameter
- displayPerson(User);
- return 0;
- }
- // Function displayPerson declaration
- // Takes structure Person as parameter
- void displayPerson(struct Person c){
- printf("Outputting information stored in structure\n");
- // Output name member
- printf("Your name is: %s\n",c.name);
- // Output age member
- printf("You are %d years old\n", c.age);
- // Output telNumber member
- printf("Your telephone number is %s\n", c.telNumber);
- }
Add Comment
Please, Sign In to add comment