#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int getEmail(char * userEmail);
int getName(char * name, char * prompt);
int getPhoneNumber(char * userPhone);
char getUserAction(void);
void addRecord(void);
void printRecords(void);
struct userRecord {
char userEmail[80];
char firstName[80];
char lastName[80];
char userPhone[80];
struct userRecord * nextRecord;
};
struct userRecord * headPtr = NULL;
struct userRecord * tailPtr = NULL;
void main()
{
char userChoice;
do {
userChoice = getUserAction(); // 'Q' = quit, 'A' = add record, 'P' = print
switch (userChoice) {
case 'A':
addRecord();
break;
case 'P':
printRecords();
break;
}
}
while (userChoice != 'Q');
}
int getName(char * charArray, char * msg)
{
int entryValid = 1; // presume entry is valid until we disprove it
int entryLength,i;
printf(msg);
gets(charArray);
printf("You entered: %s\n",charArray);
// strlen gives the length of an array of characters
entryLength = strlen(charArray); // "John" strlen = 4
// if user just pressed 'enter', this is not valid
if (entryLength == 0) {
return 0;
}
// to get here, something must have been entered
// look at each character individually
entryValid = 1;
for (i = 0; i < entryLength; i++) { // "John" i = 0,1,2,3
// if a space is in the name, invalidate it
if (charArray[i] == ' ') {
entryValid = 0;
}
// check if entry is a character
// if (!x) is the same as if (x == 0)
if (!isalpha(charArray[i])) { // !0 is equal to 1
entryValid = 0;
}
}
return entryValid;
}
int getEmail(char * charArray)
{
int entryValid = 1; // presume entry is valid until we disprove it
int entryLength,i;
printf("Enter the email address: ");
gets(charArray);
printf("You entered: %s\n",charArray);
// strlen gives the length of an array of characters
entryLength = strlen(charArray); // "John" strlen = 4
// if user just pressed 'enter', this is not valid
if (entryLength == 0) {
return 0;
}
// to get here, something must have been entered
// look at each character individually
// valid email rules:
// must not contain spaces
// must contain both the @ and the '.'
entryValid = 1;
// strchr(array,character) returns pointer to character in the array
if ( !strchr(charArray,'@') || !strchr(charArray,'.') || strchr(charArray,' ')) {
entryValid = 0;
}
return entryValid;
}
int getPhoneNumber(char * userEntry)
{
// allow 3 formats, spaces allowed
// format 1: (555) 123 - 4567
// format 2: 555 -123 -4567
// format 3: 123 - 4567
// first strip out spaces, compressing string to have no spaces
// 555 - 123 -4567 will become 555-123-4567
char * tempPtr;
char * spaceRemovalPtr;
char shiftChar;
tempPtr = userEntry;
printf("Enter user phone number: ");
gets(userEntry);
while (*tempPtr != NULL) {
printf("char: %c\n",*tempPtr);
// see if the character is a space
if (*tempPtr == ' ') {
printf("You found a space\n");
// now we need to shift everything remaining left one space
spaceRemovalPtr = tempPtr;
while (*spaceRemovalPtr != NULL) {
shiftChar = *(spaceRemovalPtr + 1);
*spaceRemovalPtr = shiftChar;
spaceRemovalPtr++;
}
printf("Adjusted string: %s\n",userEntry);
}
else {
tempPtr++;
}
}
printf("final string: %s\n",userEntry);
// at this point, we have removed all spaces
// allow 3 formats
// format 1: (555)123-4567
// format 2: 555-123-4567
// format 3: 123-4567
// if format 1 fits, return 1
if (
(userEntry[0] == '(') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] >= '0' && userEntry[3]<='9') &&
(userEntry[4] ==')') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] >= '0' && userEntry[7]<='9') &&
(userEntry[8] == '-') &&
(userEntry[9] >= '0' && userEntry[9]<='9') &&
(userEntry[10] >= '0' && userEntry[10]<='9') &&
(userEntry[11] >= '0' && userEntry[11]<='9') &&
(userEntry[12] >= '0' && userEntry[12]<='9'))
{
return 1;
}
// if format 2 fits, return 1
// 123-456-7890
if (
(userEntry[0] >= '0' && userEntry[0]<='9') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] == '-') &&
(userEntry[4] >= '0' && userEntry[4]<='9') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] == '-') &&
(userEntry[8] >= '0' && userEntry[8]<='9') &&
(userEntry[9] >= '0' && userEntry[9]<='9') &&
(userEntry[10] >= '0' && userEntry[10]<='9') &&
(userEntry[11] >= '0' && userEntry[11]<='9'))
{
return 1;
}
// if format 3 fits, return 1
if (
(userEntry[0] >= '0' && userEntry[0]<='9') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] == '-') &&
(userEntry[4] >= '0' && userEntry[4]<='9') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] >= '0' && userEntry[7]<='9'))
{
return 1;
}
// nothing fit, return 0 (bad entry)
return 0;
}
char getUserAction(void) {
char userSelection;
printf("\n(A)dd record, (P)rint list, (Q)uit: ");
userSelection = getchar();
if (userSelection != '\n') { // consume the following newline
getchar();
}
return userSelection;
}
void addRecord(void) {
int nameValid;
int emailValid;
int phoneValid;
struct userRecord * newRecPtr;
printf("in addRecord function\n");
newRecPtr = (struct userRecord *) malloc(sizeof (struct userRecord));
// get info from user
do {
phoneValid = getPhoneNumber(newRecPtr->userPhone);
if (phoneValid != 1) {
printf("You entered an invalid phone, try again\n");
}
}
while (phoneValid == 0);
do {
nameValid = getName(newRecPtr->firstName,"Enter first name: ");
if (nameValid != 1) {
printf("You entered an invalid name, try again\n");
}
}
while (nameValid == 0);
do {
nameValid = getName(newRecPtr->lastName,"Enter last name: ");
if (nameValid != 1) {
printf("You entered an invalid name, try again\n");
}
}
while (nameValid == 0);
// to get here, we now have a good first and last name
do {
emailValid = getEmail(newRecPtr->userEmail);
if (emailValid != 1) {
printf("You entered an invalid email, try again\n");
}
}
while (emailValid == 0);
newRecPtr->nextRecord = NULL;
// if no list exists yet, create first record
if (headPtr == NULL) {
printf("adding first record\n");
headPtr = newRecPtr;
tailPtr = headPtr;
}
else {
// add record at the end
// point old last record to new last record
tailPtr->nextRecord = newRecPtr;
tailPtr = newRecPtr;
}
}
void printRecords(void) {
printf("in printRecords function\n");
}