Thread: Hi guys. Address book problem:

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    33

    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.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define max_num 30
    
    void add_contact();
    void edit_contact();
    void del_contact();
    void list_contact();
    
    int main()
    {
    
    char mode, size;
    char addressbook[6][30][30];
    
        printf("Select the mode you want\n");
        printf("1 -> Add contact\n"); 
        printf("2 -> Edit contact\n");
        printf("3 -> Delete contact\n");
        printf("4 -> List contact\n");
        printf("5 -> Exit the program\n");
        scanf("%c", &mode);
        
        switch(mode){ //switch (determine which action to take
        
        case 1:
        add_contact();
        break
        case 2:
        edit_contact();
        break;s
        case 3:
        del_contact();
        break;
        case 4:
        list_contact();
        break;
        } //end switch
        return 0;   
    }

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Quote Originally Posted by jwroblewski44 View Post
    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?

    This is like a test code in creating my function add_contact

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by johnjrgs View Post
    It doesn't compile. It says there's an error in function 'main': syntax error before return. How do I fix this?
    Start from the line your compiler tells you and look upwards. Do you see any syntax errors? Hint: all statements must end in a semicolon.

    Also, don't add comments like "// end for" to the ending brace. If you need a comment to tell you this, it probably means your indentation is not adequate.

    Example:
    Code:
    for (...) {
    for (...) {
    for (...) {
    ...
    } // end for
    } // end for
    } // end for
    In the above snippet, the comments are used to clarify where the for loops end. But indentation is better used for this purpose:
    Code:
    for (...) {
        for (...) {
            for (...) {
                ...
            }
        }
    }

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    33
    Cool thanks. I'll try to build up such a habit. xD

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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."); 
            }
    Code:
       // Either:
       if(condition) {
          // do stuff.
       }
       else {
          // do something else.
       }
      // OR:
       if(condition) 
       {
          // do stuff.
       }
       else 
       {
          // do something else.
       }

    As to your error, it looks like you are missing a semicolon terminating your do/while loop.

    Jim

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    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.

    something like:
    Code:
    while( choice != exit )
    {
            get choice from user;
            if( add contact )
            {
                     while( there is data in input )
                     {
                            read in data;                        //scanf()
                            check for invalid characters; //isalpha()
                     }
            }
    }
    and so on...
    This will hopefully help you organize your logic and thus lead you into function creation. Here's an excellent link to aid your research.
    Functions in C - Cprogramming.com
    Last edited by jwroblewski44; 02-17-2013 at 08:20 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you guys think this book is outdated?
    By Syscal in forum Windows Programming
    Replies: 13
    Last Post: 11-17-2010, 07:51 AM
  2. address book
    By cboard in forum C Programming
    Replies: 10
    Last Post: 04-05-2007, 11:47 PM
  3. Replies: 5
    Last Post: 09-19-2004, 06:52 AM
  4. address book problem
    By datainjector in forum C Programming
    Replies: 13
    Last Post: 11-27-2002, 08:14 PM
  5. Address book
    By sundeeptuteja in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2002, 02:40 PM