![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 3
| Help on Program 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 |
| sharpshot67 is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 2,506
| 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. |
| Adak is online now | |
| | #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");
}
|
| sharpshot67 is offline | |
| | #4 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| 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)); here Additem Code: conlist=AddItem (con,p.lastName); [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 |
| RockyMarrone is offline | |
| | #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 |
| sharpshot67 is offline | |
![]() |
| Tags |
| basic, chelp, helpme, noob, phonebook |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Issue with program that's calling a function and has a loop | tigerfansince84 | C++ Programming | 9 | 11-12-2008 01:38 PM |
| Need help with a program, theres something in it for you | engstudent363 | C Programming | 1 | 02-29-2008 01:41 PM |
| This is a simple program.. Help me please I think it has an error.. | lesrhac03 | C Programming | 4 | 02-21-2008 10:39 AM |
| My program, anyhelp | @licomb | C Programming | 14 | 08-14-2001 10:04 PM |