UMBC CS 201, Fall 06
UMBC CMSC 201
Fall '06

CSEE | 201 | 201 F'06 | lectures | news | help

Structure Example

The Code

/************************************* ** File: easystruct.c ** Author: Sue Bogar ** Date: 10/10/00 ** Section: 0101 ** E-Mail: bogar@cs.umbc.edu ** Modified by: Sue Evans ** Date: 2/29/04 ** Modified to meet current 201 standards ** ** This file demonstrates the basics ** of structures. ***************************************/ #include <stdio.h> #define FRESHMAN 1 #define SOPHOMORE 2 #define JUNIOR 3 #define SENIOR 4 /* structure definition */ /* A student stucture */ struct student { char lastName[25]; char firstName[25]; char middleInitial[2]; char ssn[12]; int homeStreetNum; char homeStreetName[25]; char homeCity[25]; char homeState[3]; int homeZipCode; char homePhone[14]; char dorm[25]; int roomNum; char campusPhone[14]; int rank; char major[5]; char minor[5]; float gpa; }; struct student GetStudentInfo (void); void PrintStudentInfo (struct student s); struct student ChangeHomePhone (struct student s); int main ( ) { struct student student1; student1 = GetStudentInfo ( ); PrintStudentInfo (student1); student1 = ChangeHomePhone (student1); PrintStudentInfo (student1); return 0; } /******************************************************** * GetStudentInfo * prompts the user for all of the information that is * needed about a student, fills the members of a struct * student and returns the struct. * Input : none * Output: a struct student ********************************************************/ struct student GetStudentInfo (void) { struct student s; /* Student identification information */ printf("Enter the student's last name :"); scanf ("%s", s.lastName); printf("Enter the student's first name :"); scanf ("%s", s.firstName); printf("Enter the student's middle initial :"); scanf ("%s", s.middleInitial); printf("Enter the student's social security number :"); scanf ("%s", s.ssn); /* Home information */ printf("Student's home address : \n"); printf("Enter the house number : " ); scanf ("%d", &s.homeStreetNum); printf("Enter the street name :"); scanf ("%s", s.homeStreetName); printf("Enter the city :"); scanf ("%s", s.homeCity); printf("Enter the 2-letter state abbreviation :"); scanf ("%s", s.homeState); printf("Enter the zip code :"); scanf ("%d", &s.homeZipCode); printf("Enter the home phone number :"); scanf ("%s", s.homePhone); /* Campus Information */ printf("Campus information :\n"); printf("Enter the dorm name :"); scanf ("%s", s.dorm); printf ("Enter the room number :"); scanf ("%d", &s.roomNum); printf("Enter the campus phone number :"); scanf ("%s", s.campusPhone); /* Academic Information */ printf("Enter the class rank 1 - 4 :"); scanf ("%d", &s.rank); printf("Enter the 4-letter major designation :"); scanf ("%s", s.major); printf("Enter the 4-letter minor designation :"); scanf ("%s", s.minor); printf("Enter the gpa :"); scanf ("%f", &s.gpa); return s; } /******************************************************** * PrintStudentInfo * Prints out all of a student's information * Input : a struct student * Output: none ********************************************************/ void PrintStudentInfo (struct student s) { printf("\n\nSTUDENT INFORMATION\n\n"); printf("Student identification information :\n"); printf("Name : %s, %s, %s.\n", s.lastName, s.firstName, s.middleInitial); printf("SSN : %s\n\n", s.ssn); printf("Home information :\n"); printf("Home address : %d %s\n", s.homeStreetNum, s.homeStreetName); printf(" %s, %s %d\n", s.homeCity, s.homeState, s.homeZipCode); printf("Home phone : %s\n\n", s.homePhone); printf("Campus information :\n"); printf("Dorm room : %s - %d\n", s.dorm, s.roomNum); printf("Campus phone : %s\n\n", s.campusPhone); printf("Academic Information :\n"); printf("Class rank : "); switch (s.rank) { case FRESHMAN: printf("Freshman\n"); break; case SOPHOMORE: printf("Sophomore\n"); break; case JUNIOR: printf("Junior\n"); break; case SENIOR: printf("Senior\n"); break; default: printf("Error in rank\n"); break; } printf("Major : %s\n", s.major); printf("Minor : %s\n", s.minor); printf("GPA : %.4f\n\n", s.gpa); } /******************************************************** * ChangeHomePhone * prompts the user for a new home phone number and * stores it in the struct student that is passed in, * then returns the struct. * Input : the struct student to be modified * Output: the struct student that has been modified ********************************************************/ struct student ChangeHomePhone (struct student s) { printf("\n\nCHANGE PHONE NUMBER\n\n"); printf("Enter new phone number in the form 111-222-3333 : "); scanf("%s", s.homePhone); return s; }

The Output

linux1[102] % a.out Enter the student's last name :Doe Enter the student's first name :John Enter the student's middle initial :S Enter the student's social security number :111-22-3333 Student's home address : Enter the house number : 5 Enter the street name :Main Enter the city :Baltimore Enter the 2-letter state abbreviation :MD Enter the zip code :21201 Enter the home phone number :410-555-1212 Campus information : Enter the dorm name :Patapsco Enter the room number :215 Enter the campus phone number :443-555-3333 Enter the class rank 1 - 4 :2 Enter the 4-letter major designation :CMSC Enter the 4-letter minor designation :MATH Enter the gpa :3.5 STUDENT INFORMATION Student identification information : Name : Doe, John, S. SSN : 111-22-3333 Home information : Home address : 5 Main Baltimore, MD 21201 Home phone : 410-555-1212 Campus information : Dorm room : Patapsco - 215 Campus phone : 443-555-3333 Academic Information : Class rank : Sophomore Major : CMSC Minor : MATH GPA : 3.5000 CHANGE PHONE NUMBER Enter new phone number in the form 111-222-3333 : 410-555-1111 STUDENT INFORMATION Student identification information : Name : Doe, John, S. SSN : 111-22-3333 Home information : Home address : 5 Main Baltimore, MD 21201 Home phone : 410-555-1111 Campus information : Dorm room : Patapsco - 215 Campus phone : 443-555-3333 Academic Information : Class rank : Sophomore Major : CMSC Minor : MATH GPA : 3.5000 linux1[103] %


CSEE | 201 | 201 F'06 | lectures | news | help

Tuesday, 22-Aug-2006 07:14:09 EDT