Help on Program

This is a discussion on Help on Program within the C Programming forums, part of the General Programming Boards category; Hello, I am new to this forum and I don't really know exactly how this works. I am wondering if ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Help on Program

    Hello, I am new to this forum and I don't really know exactly how this works. I am wondering if anyone who is good with C Programming could help me, I am very basic at this and I am taking a class in college and totally lost :P. But here we go, and Thanks ahead for any type of help, I thoroughly appreciate it.

    The Program: A basic phonebook written in C. The use has to add contact, delete, and show the contacts utilizing: structs and dynamic memory allocation.

    I have written this code:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //Defining my variables

    int entry;
    int contactsnum=0;
    int i=0;

    //Defining my struct

    typedef struct phonebook
    {
    char firstName[20];
    char lastName[20];
    char number[20];
    }con;

    //Attempting to create a code for adding and removing items in con struct
    con * AddItem (con * conlist);
    con * RemoveItem (con * conlist);

    //main function

    main(){
    phonebook entry[100];
    phonebook* contacts;
    con=(phonebook*)malloc(sizeof(con)*sizeof(con));
    //problem with this line compiler won't compile
    con p;
    int choice=0;


    //A do while loop allows for constent adding and remove

    do{
    con listmember, *conlist;
    char firstName[20];
    char lastName[20];
    char number[11];

    printf("Hello, Welcome to Command Prompt Phonebook.\n");
    printf("Please Select an option:\n");
    printf("(1) Add Contact\n");
    printf("(2) Delete Contact\n");
    printf("(3) Search Phonebook\n");
    printf("(4) Exit Program\n");
    printf("Enter Selection:");
    scanf("%d",&choice);

    //switch for choices

    switch(choice){
    case 1:
    printf("\nYou chose Add Contact:\n");
    printf("\nPlease Enter First Name:");
    scanf("%s",&p.firstName);
    conlist=AddItem (contact,p.firstName);
    printf("\nPlease Enter Last Name:");
    scanf("%s",&p.lastName);
    conlist=AddItem (con,p.lastName);
    printf("\nPlease Enter Phone Number:");
    scanf("%s",&p.number);
    conlist=AddItem (con, number);
    printf("\nNumber Added to Phonebook");
    break;
    /* The code I wrote here doesn't work. I get "expected
    primary-expresion before '=' token" I don't really understand
    whats wrong with it */

    case 2:
    printf("You chose Delete Contact:");
    printf("Please Enter First Name:");
    scanf("%d",&p.firstName);
    conlist=RemoveItem (con,p.firstName);
    printf("Please Enter Second Name:");
    scanf("%d",&p.lastName);
    conlist=RemoveItem (con,p.lastName);
    printf("Please Enter Number:");
    scanf("%f",&p.number);
    conlist=RemoveItem (con,p.number);
    printf("Number has been deleted");
    break;
    // Same problem

    case 3:
    printf("\nCommand Prompt Phonebook Contents:");
    printf("\nFirst Name:%d",&p.firstName);
    printf("\nLast Name:%d",&p.lastName);
    printf("\nPhone Number:%f",&p.number);
    break;
    // Haven't really worked at this yet

    default:
    printf("Please Enter Valid Number");

    case 4:
    break;
    }
    }while(choice!=4);
    printf("\nThanks for using! Goodbye!\n");

    system("pause");
    }


    I have many problems in my syntax I assume, but being very basic I have no idea what to do. I would really apprecaite help; but, I'm sure this board gets many questions like this so I don't expect alot of help. I do thoroughly apprecaite even slight help. I'm not asking for you to write it how it SHOULD be but point me in the direct I need to look.

    Thanks Again,

    Ryan

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Ryan!

    Whenever you post code, highlight it the code, and then click on the # icon of the window where you enter text.

    That makes your code *MUCH* easier to read.

    Please edit your last post, and add the code tags (as they're called), around your program.

    I'll look at it in a bit.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    My apologies

    Thanks for the welcome, I assume I will be using this resource alot during my time at CECS school haha.Thanks for the responce and I apologize for not knowing but here is the code again:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    //Defining my variables
    
    int entry;
    int contactsnum=0;
    int i=0;
    
    //Defining my struct
    
    typedef struct phonebook
    {
    char firstName[20];
    char lastName[20];
    char number[20];
    }con;
    
    //Attempting to create a code for adding and removing items in con struct
    con * AddItem (con * conlist);
    con * RemoveItem (con * conlist);
    
    //main function
    
    main(){
    phonebook entry[100];
    phonebook* contacts;
    con=(phonebook*)malloc(sizeof(con)*sizeof(con));
    //problem with this line compiler won't compile
    con p;
    int choice=0;
    
    
    //A do while loop allows for constent adding and remove
    
    do{ 
    con listmember, *conlist;
    char firstName[20];
    char lastName[20];
    char number[11];
    
    printf("Hello, Welcome to Command Prompt Phonebook.\n");
    printf("Please Select an option:\n");
    printf("(1) Add Contact\n");
    printf("(2) Delete Contact\n");
    printf("(3) Search Phonebook\n");
    printf("(4) Exit Program\n");
    printf("Enter Selection:");
    scanf("%d",&choice);
    
    //switch for choices
    
    switch(choice){
    case 1:
    printf("\nYou chose Add Contact:\n");
    printf("\nPlease Enter First Name:");
    scanf("%s",&p.firstName);
    conlist=AddItem (contact,p.firstName);
    printf("\nPlease Enter Last Name:");
    scanf("%s",&p.lastName);
    conlist=AddItem (con,p.lastName);
    printf("\nPlease Enter Phone Number:");
    scanf("%s",&p.number);
    conlist=AddItem (con, number);
    printf("\nNumber Added to Phonebook");
    break;
    /* The code I wrote here doesn't work. I get "expected
    primary-expresion before '=' token" I don't really understand
    whats wrong with it */
    
    case 2:
    printf("You chose Delete Contact:");
    printf("Please Enter First Name:");
    scanf("%d",&p.firstName);
    conlist=RemoveItem (con,p.firstName);
    printf("Please Enter Second Name:");
    scanf("%d",&p.lastName);
    conlist=RemoveItem (con,p.lastName);
    printf("Please Enter Number:");
    scanf("%f",&p.number);
    conlist=RemoveItem (con,p.number);
    printf("Number has been deleted");
    break;
    // Same problem
    
    case 3:
    printf("\nCommand Prompt Phonebook Contents:");
    printf("\nFirst Name:%d",&p.firstName);
    printf("\nLast Name:%d",&p.lastName);
    printf("\nPhone Number:%f",&p.number);
    break;
    // Haven't really worked at this yet
    
    default:
    printf("Please Enter Valid Number");
    
    case 4:
    break;
    }
    }while(choice!=4);
    printf("\nThanks for using! Goodbye!\n"); 
    
    system("pause");
    }
    Thanks again for all the help!

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    There are so many problems dude in the code rectify one by one i can pinpoint one or two here by just one go through here

    if u r compiling your code with gcc the put in the following lines struct like
    Code:
    struct phonebook entry[100];
    struct phonebook* contacts;
    Code:
    con=(phonebook*)malloc(sizeof(con)*sizeof(con));
    Don't know what you want to do with above piece of code. as you already said con is global structure object that will be available through the file why u wanna allocate some ........ peice of memory to don't understand that

    here Additem
    Code:
    conlist=AddItem (con,p.lastName);
    is taking two parameters while the signature obove is taking one parameter how the compiler can compile this god knows
    [code]
    //Attempting to create a code for adding and removing items in con struct
    con * AddItem (con * conlist);
    con * RemoveItem (con * conlist);

    [/code


    try to solve these kind of bugs if the problem will not solve please post again the code with modifications

    c y

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Haha! I know its completely filed with problems. This is my first time learning C Programing and I have absolutely no idea what I'm doing. I really appreciate the help, I will go back and try to get it working, or at least half way because todays the day to send her in.

    Thanks again for all the help!

    Ryan

    P.S. I will be back numerous times I can already tell. I love this place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21