Hi guys. Address book problem:

This is a discussion on Hi guys. Address book problem: within the C Programming forums, part of the General Programming Boards category; I'm still new to C and I need help creating the functions to be used in my code for my ...

Hi guys. Address book problem:

I'm still new to C and I need help creating the functions to be used in my code for my project.
So far this is what I've written. Here are the parameters/objectives:

OVERVIEW:
Create a simple address book program that stores the first name, last name, student number, degree
program, mobile number and email address of a student. The program will have five possible actions:
add a new entry, edit an existing entry, delete an existing entry, list all entries, and close the program.
The program should keep asking for the next action until the user closes the program.

CONSTRAINTS:
• Aside from your own functions, you are only allowed to use the functions in the following
libraries:
• stdio.h
• ctype.h
• string.h
• There should be a macro in your code defining the number of entries in your address book. The
maximum allowable number of entries is 30.
• There should be no recursive functions.
• The inputs should have the following error checking capabilities:
• first name, last name, degree program: can only contain letters and spaces, max of 20, at
least one character
• student number: can only contain digits and a dash, format is xxxx-yyyyy
• mobile number: can only contain digits, format is 09xxyyyyyyy
• email: can only contain letters, digits, underscores, dots, and @, max of 30
• Implement this using only the concepts discussed from lectures 1 to 8a.

It's hard without knowing what concepts are off the table. It seems that a data struct would be nice here to hold and contain all the student object data. This way we can specify the length of each specific data member. If linked lists and dynamic memory are available, there's your answer.

It's hard without knowing what concepts are off the table. It seems that a data struct would be nice here to hold and contain all the student object data. This way we can specify the length of each specific data member. If linked lists and dynamic memory are available, there's your answer.

I see. So using struct would be better? I'll try using it.

I'm stuck with with the function add_contact though:

Here's what I got so far..

Code:

#include <string.h>
#include <stdio.h>
#include <ctype.h>
int main()
{
char addressbook[6][30][0];
int current_record = 0, length, i=0;
int address_count = 0;
current_record = address_count-1; //variable for the current entry
do{
printf("Enter the first name:"); //asks the user to input the first name of the entry
scanf("%s", addressbook[0][current_record]);
length = strlen(addressbook[0][current_record]); //gets the length of the input (should be less than 20)
for(i=0; i<=20; i++){
if(isalpha(addressbook[0][current_record][i])||addressbook[0][current_record][i]==' ')
{ //checks if the input characters in first name are alphabets/a space
break; //
} //exits once it sees that the characters are alphabets/space
else {
printf("You can only use alphabets and space and a maximum of 20 characters. Input again."); //puts a message that prompts the user to enter alphabets/space alone
} //end else
} // end for
} //end do
while(length<20)
return 0;
}

It doesn't compile. It says there's an error in function 'main': syntax error before return. How do I fix this?

Also be consistent about where you place the braces, in the following snippet you place the brace in different locations.

Code:

if(isalpha(addressbook[0][current_record][i])||addressbook[0][current_record][i]==' ')
{ // Brace at the same level as your if statement.
break;
}
else { // But else has brace at the end of the else. Be consistent. Either put this on a line of it's own or put the if brace at the end of the if.
printf("You can only use alphabets and space and a maximum of 20 characters. Input again.");
}

Something I noticed is that on line 10 you are essentially setting current_record to -1 (negative one). Then attempting to place data into addressbook[0][current_record]. I dont think this is what you're trying to do.

It also looks like you never increase the first index number in to the address book array in your for loop. This must be done or you will just keep rewriting over the same space.

You should start with your word processor and write out some pseudo code explaining exactly what needs to be done in which steps. Start thinking what code portions should be inside which function.