Warning
This page is located in archive.

Lab 05 - Structs and enums

Task 1

Build a contact system/address book.

Write a program which allows users to enter information about contacts (eg. name, age, address, date of birth etc.) You should store this information in a struct.

The user can then add more contacts (hint: dynamic memory!)

The user can also view all saved contacts. It will loop through the contacts.

Here's an example using only age:

Enter 1 to add new contact
Enter 2 to view contacts
Enter 3 to exit
Choice: 1

Creating new user
Enter age:23

Enter 1 to add new contact
Enter 2 to view contacts
Enter 3 to exit
Choice: 1

Creating new user
Enter age:28

Enter 1 to add new contact
Enter 2 to view contacts
Enter 3 to exit
Choice: 2

===User 0===
Age: 23


===User 1===
Age: 28


Enter 1 to add new contact
Enter 2 to view contacts
Enter 3 to exit
Choice: 3

Task 2

Building on the previous program:

Allow users to delete a contact (ie. type the index of the user to delete…)

Use Valgrind to verify your program is running without memory leaks.

See if you can use enums to store the date value.

Don’t forget!

Enums allow us to interact with numbers in a more programmer friendly manner. While still numbers underneath and internally, we can effectively ‘nickname’ these numbers. Consider the names of months. We can create enum year{Jan, Feb, …}. Where Jan is effectively a zero. This allows us to do things like: for (i=Jan; i⇐Dec; i++) printf(“%d ”, i);

courses/be5b99cpl/labs/lab05.txt · Last modified: 2023/10/24 13:07 by brouggeo