-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.h
96 lines (88 loc) · 3.09 KB
/
main.h
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
#include "common.h"
#include "quiz.h"
#include "admin.h"
#include "login.h"
struct Quizlist quizlist;
struct User userlist[max_users];
struct User currentuser;
struct Question question[max_quizes][max_q_per_quiz][max_alternative_q]; //Questions and their alternatives
struct Response response[max_users][max_quizes][max_q_per_quiz][max_alternative_q]; //Responses (index should be user ID)
struct Quizes_attempted quizes_attempted[max_users][max_quizes]; //attempts related to a quiz are stored at quiz’s Id.
int login_status;
int no_of_currentusers;
char taglist[max_tags][13];
time_t start=0, current=0, last;
void clearscr(void) {
#ifdef _WIN32
system("cls");
#elif defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
system("clear");
#else
#error "OS not supported."
#endif
}
void clearBuf() { //Cleans buffer (To be used after scanf)
char ch;
while(ch = getchar(), ch != '\n' && ch != EOF);
}
void smart_fgets(char *str, int n, FILE *stream) { //Alternative to fgets to ensure buffer is clean
fgets(str, n, stream);
int len = strlen(str);
while (str[0]==' ') { //Remove any heading whitespaces
for (int i = 0; i < len; ++i) {
str[i]=str[i+1];
}
len--;
}
if ((strlen(str)>1) && (str[len-1]=='\n')) { //No input overflow
str[strlen (str) - 1] = '\0'; //Get rid of terminating '\n'
}
else if (len==n-1) { //Input has overflowed
clearBuf(); //Clean Buffer
}
else if (len<=1) {
printf("%sEmpty input, please try again!\n%s",red,normal);
smart_fgets(str,n,stream);
}
}
int scanf_int(int uplimit,int lowlimit){ //upper limit is first arg and lower limit is second arg
int res;
int out;
out = scanf("%d",&res);
clearBuf();
if(out == 1){
if(res <= uplimit && res >= lowlimit) return res; //checks whether the input is in range or not.
}
printf("\n%sNot a valid response try again (should be a number between %d and %d)%s\n",red,lowlimit,uplimit,normal);
scanf_int(uplimit,lowlimit);
}
void wait_for_enter() {
char a[1];
smart_fgets(a, 1, stdin);
}
void multiline_input(char *arr,int len) { //takes multiple lines from user.user needs to press enter 2 times to end input session. len >4 always.
int lastindex = 0;
for (int index = 0; index < len; index++)
{
arr[index] = getchar();
if(index == 0){
if(arr[0]== '\n'){
printf("%sEmpty input try agian!%s\n",red,normal);
multiline_input(arr,len);
break;
}
}
if(index >1){
if( arr[index] == '\n' && arr[index-1] =='\n' && arr[index-2]=='\n'){ //checking for 3 adjacent \n s from input.
arr[index-2] = '\0';
break;
}
}
lastindex ++;
}
if(lastindex == len){
clearBuf();
printf("%sNo.of characters exceeded the limit try again!%s\n",red,normal);
multiline_input(arr,len);
}
}