forked from morrismukiri/C-Structure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
assignment.c
170 lines (135 loc) · 3.27 KB
/
assignment.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* File: assignment.c
* Author: morris
*
* Created on March 24, 2015, 11:51 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SCREEN_HEIGHT 25
void menu(int option); //main menu
void cls(); //clear screen
void addStudent();
void searchStudent();
void editStudent();
void deleteStudent();
int validScore(int score); //validate whether score is 0-100
int main() {
menu(0);
}
void menu(int option) {
if (option == 0) {
//cls();
printf("\n0. Exit");
printf("\n1. Add Student");
printf("\n2. Search Student");
printf("\n3. Edit Student");
printf("\n4. Delete Student\n");
printf("Enter option:");
option = getchar();
option -= 48; //convert to int
}
switch (option) {
case 0:
break;
case 1:
addStudent();
menu(0);
case 2:
searchStudent();
menu(0);
case 3:
editStudent();
menu(0);
case 4:
deleteStudent();
menu(0);
default:
menu(0);
break;
}
}
void addStudent() {
char Fname[20], Lname[20], reg[10];
int eng, kisw, math, sci, soci;
float total, average;
printf("\nADD STUDENT...\n");
printf("\nEnter Student Registration number:");
scanf("%s", ®);
printf("\nEnter Student first name:");
scanf("%s", &Fname);
printf("\nEnter Student last name:");
scanf("%s", &Lname);
do {
printf("\nEnter English Score:");
scanf("%d", &eng);
} while (!validScore(eng));
do {
printf("\nEnter Kiswahili Score:");
scanf("%d", &kisw);
} while (!validScore(kisw));
do {
printf("\nEnter Mathematics Score:");
scanf("%d", &math);
} while (!validScore(math));
do {
printf("\nEnter Science Score:");
scanf("%d", &sci);
} while (!validScore(sci));
do {
printf("\nEnter Social studies Score:");
scanf("%d", &soci);
} while (!validScore(sci));
total = eng + kisw + math + sci + soci;
average = total / 5;
char dbOutput[150];
snprintf(dbOutput, sizeof dbOutput, "%s,%s,%s,%d,%d,%d,%d,%d,%.2f,%.2f", reg, Fname, Lname, eng, kisw, math, sci, soci,total,average);
if(insertRecord(dbOutput)){
printf("\n Student added Successfully\n");
}else{
printf("Error adding student\n");
}
}
char* findRecord(char *what, int lineColumn) {
char result[150];
return " ";
}
int insertRecord(char *data) {
int result = 1;
FILE *db;
db = fopen("db.txt", "a+");
strcat(data, "\n");
int res = fprintf(db, data);
if (res< sizeof data) {
result = 0;
}
fclose(db);
return result;
}
int deleteRecord(int index) {
}
int validScore(int score) {
int valid = 0;
if (score >= 0 && score <= 100) {
valid = 1;
} else {
printf("\nINVALID SCORE! Score should be betwen 0-100\n");
}
return valid;
}
void searchStudent() {
printf("SEARCH STUDENT...\n");
}
void editStudent() {
printf("EDIT STUDENT...\n");
}
void deleteStudent() {
printf("DELETE STUDENT...\n");
}
void cls() {
int i;
for (i = 0; (i <= SCREEN_HEIGHT); i++) {
printf("\n");
}
}