Created
February 17, 2017 05:50
-
-
Save kanzash/879b46a7b76c09269680355bc76153ca to your computer and use it in GitHub Desktop.
Allows you to create a database of students with their names, ID number and e-mail. Dynamically allocates enough space for unlimited number of students to be entered. Error handling for incorrect IDs and names that are too long/nonexistent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
#define ROW 50 | |
#define COL 900 | |
int main () { | |
struct student{ | |
int recordCount; | |
char *firstName; | |
char *lastName; | |
char *id; | |
char *email; | |
}; | |
struct student *array; | |
struct student temp; | |
array = malloc(sizeof(struct student)); | |
int i = 0; | |
int k = 0; | |
char myFirst[ROW][COL]; | |
char myLast[ROW][COL]; | |
char myID[ROW][COL]; | |
char myEmail[ROW][COL]; | |
do { | |
/** Getting first name **/ | |
printf("First Name: "); | |
fgets(myFirst[i], 51, stdin); | |
myFirst[i][strlen(myFirst[i]) + 1] = '\0'; | |
/** termination condition **/ | |
if (strncmp(myFirst[i], ".", 1) == 0) | |
{ | |
break; | |
} | |
/** error checking first name **/ | |
while (strlen(myFirst[i]) > 50) | |
{ | |
printf("Name too long, please try again\n"); | |
printf("First Name: "); | |
fgets(myFirst[i], 51, stdin); | |
myFirst[i][strlen(myFirst[i]) + 1] = '\0'; | |
} | |
/** getting last name **/ | |
printf("Last Name: "); | |
fgets(myLast[i], 51, stdin); | |
myLast[i][strlen(myLast[i]) + 1] = '\0'; | |
/** error checking for last name **/ | |
while (strlen(myLast[i]) > 50) | |
{ | |
printf("Name too long, please try again\n"); | |
printf("Last Name: "); | |
fgets(myLast[i], 51, stdin); | |
myLast[i][strlen(myLast[i]) + 1] = '\0'; | |
} | |
/** getting ID number **/ | |
printf("ID#: "); | |
fgets(myID[i], 20, stdin); | |
myID[i][strlen(myID[i]) + 1] = '\0'; | |
/** error checking ID number **/ | |
while (strlen(myID[i]) < 10 || strlen(myID[i]) >= 11) | |
{ | |
printf("ID must be 9 digits, please try again\n"); | |
printf("ID#: "); | |
fgets(myID[i], 20, stdin); | |
myID[i][strlen(myID[i]) + 1] = '\0'; | |
} | |
for (k=0; k<10; k++){ | |
while (isalpha(myID[i][k])) { | |
printf("No alphabet allowed\n"); | |
printf("ID#: "); | |
fgets(myID[i], 20, stdin); | |
myID[i][strlen(myID[i]) + 1] = '\0'; | |
} | |
} | |
/** getting email **/ | |
printf("Email: "); | |
fgets(myEmail[i], 51, stdin); | |
myEmail[i][strlen(myEmail[i]) + 1] = '\0'; | |
/**iterating and assigning record number **/ | |
i++; | |
} while (strncmp(myFirst[i], ".", 1) != 0); | |
int j = 0; | |
for (j=0; j<i; j++) { | |
array[j].firstName = malloc(sizeof(char)*(strlen(myFirst[j]))); | |
array[j].firstName = myFirst[j]; | |
array[j].lastName = malloc(sizeof(char)*(strlen(myLast[j]))); | |
array[j].lastName = myLast[j]; | |
array[j].id = malloc(sizeof(char)*(strlen(myID[j]))); | |
array[j].id = myID[j]; | |
array[j].email = malloc(sizeof(char)*(strlen(myEmail[j]))); | |
array[j].email = myEmail[j]; | |
array[j].recordCount = j; | |
} | |
for (j=0; j<i; j++) { | |
for (k = 0; k < i-1; k++) { | |
if (atoi(array[k].id) < atoi(array[k+1].id)) { | |
temp = array[k]; | |
array[k] = array[k+1]; | |
array[k+1] = temp; | |
} | |
} | |
} | |
for (j=0; j<i; j++) { | |
printf("%s%s%s%s", array[j].firstName, array[j].lastName, array[j].id, array[j].email); | |
} | |
//clean up | |
for (j=0; j<i; j++) { | |
free (array[j].firstName); | |
free (array[j].lastName); | |
free (array[j].id); | |
free (array[j].email); | |
free (array); | |
} | |
return 0; | |
} | |
/***** | |
tackling | |
3. removing newline | |
4. printing with commas | |
5. limiting length | |
6. error handling | |
*****/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment