forked from malmhaug/C_AbsBegin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC9E3PhoneBook.c
85 lines (65 loc) · 1.94 KB
/
C9E3PhoneBook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* Challanges Ch9E3 - PhoneBook */
// By Jim-Kristian Malmhaug | Date: 6 August 2014
//Libraries:
#include <stdio.h>
#include <string.h>
//Structure:
typedef struct phoneBook {
char cName[25];
int iNumber;
} pBook;
//Function prototypes
int showMenu(); //Shows user options.
void addToBook(pBook *); //Function which let You add person to phone book.
//Main function
int main() {
pBook pB[5] = {"A",0};
int iOpt = 0;
do{
int j = 0;
iOpt = showMenu();
switch(iOpt) {
case 1:
for(j = 0 ; j < 5 ; j++){
if(pB[j].iNumber != 0){
printf("\n------------------------");
printf("\nName: %s", pB[j].cName);
printf("\nNumber: %d", pB[j].iNumber);
}
}
break;
case 2:
addToBook(pB);
break;
}
}while(iOpt != 3);
return 0;
}
//---------------------------------------------------------
//Function definition - showMenu()
int showMenu(){
int iOpt = 0;
printf("\n\nWelcome to Your Phone Book!");
printf("\nChoose an option:\n\n1.Print Book\n2.Add person\n3.Quit!");
printf("\n---> ");
scanf("%d", &iOpt);
return iOpt;
}
//---------------------------------------------------------
//Function definition - addToBook(pBook * p)
void addToBook(pBook * p){
int i = 0;
for (i = 0 ; i < 5 ; i++){
if(p[i].iNumber == 0){
printf("\n\nEnter name: ");
scanf("%s", p[i].cName);
printf("\nEnter number: ");
scanf("%d", &p[i].iNumber);
break;
}
if(i == 4 && p[i].iNumber != 0){
printf("\nBook is full!\n");
break;
}
}
}