I am writing a phonebook program to store names and number using a list. Here is the code for the function which allows the user to enter the name and number (where the error occurs).
Code:
//THIS FUNCTION ADDS A NEW ENTRY TO THE phonebook_list
void insert(void){
//variables
int flag=0, length=0, menu=0, i=0, check=0;
NODE *p,*q, *temp;
//USER MUST ENTER FIRST NAME ONLY FOR ADDITION TO PHONEBOOK
printf("\nPlease insert the <First Name> of the entry you would like to add not separated by a space.\n");
if((temp=(NODE *)malloc(sizeof(NODE))) == (NODE *)NULL)
{
printf("Memory could not be allocated! Returning to menu...\n\n");
return;
}
scanf("%s", temp->name);
//check if name is too long for the array IF IT IS PROMPTS USER TO RE ENTER SHORTER NAME
while(flag==0)
{
length=strlen(temp->name);
if(length<50){
for(i=0;i<length;i++)
temp->name[i] = toupper(temp->name[i]);
flag=1;
}else{
printf("\nName is too long! Please re-enter (less than 50 characters).\n");
scanf("%s", temp->name);
length=strlen(temp->name);
}
}
//prompt user for telephone number
printf("%s",temp->number);
printf("\nPlease insert the telephone number in the following format XXXXXXXXXX.\n");
scanf("%s", temp->number);
flag=0;
//check if number has the correct number of chars
while(flag==0){
length=strlen(temp->number);
if(length==10){
flag=1;
}else{
printf("\nThis is an invalid number. Please re-enter.\n");
scanf("%s", temp->number);
length=strlen(temp->number);
}
}
printf("%s", head->name);
//IF PHONEBOOK IS EMPTY THEN THE HEAD WILL BE A NULL POINTER AND TEMP NODE WILL BE ADDED
if(head==NULL)
{
if((head=(NODE *)malloc(sizeof(NODE))) == (NODE*)NULL)
{
printf("Memory could not be allocated!\nReturning to menu...\n\n");
return;
}else{
strcpy(head->number,temp->number);
strcpy(head->name,temp->name);
head->next=tail;
free(temp);
printf("Entry successfully added.\n\n");
return;
}
}else{
//IF NEW NODE IS NOT THE FIRST ENTRY(NODE) IN PHONEBOOK(LIST) WILL LOOP THROUGH PHONEBOOK TO FIND CORRECT SPOT TO ADD
p=head;
printf("%s", p->name);
while(p->next!=NULL)
{
//IF NAME IS ALREADY IN PHONEBOOK ERROR IS GIVEN AND PROGRAM RETURNS USER TO MAIN MENU
if(strcmp(p->name,temp->name)==0)
{
printf("This name already exists in phonebook!\nReturning to menu...\n\n");
return;
//OTHERWISE THE WHILE LOOP WILL CONTINUE THROUGH LIST UNTIL IT FIND THE ADDITION THAT IS ALPHABETICALLY HIGHER
//AND WILL ADD THAT NODE IN ITS CORRECT SPOT
}else if(strcmp(p->name,temp->name)<0)
{
printf("\nTRUE\n");
temp->next=p;
temp->prev=p->prev;
p->prev->next=temp;
p->prev=temp;
printf("Entry successfully added.\n\n");
return;
}
p=p->next;
}
//IF NO NAME IS ALPHABETICALLY HIGHER, THAN IT SHOULD REACH THE LAST NODE AND WILL BE ADDED TO THE END OF THE PHONEBOOK(LIST)
if(p->next==NULL)
{
if((temp=(NODE *)malloc(sizeof(NODE))) == (NODE *)NULL)
{
printf("Memory could not be allocated!\nReturning to menu...\n\n");
return;
}
temp->next=tail;
temp->prev=q;
q->next=temp;
free(temp);
printf("Entry successfully added.\n\n");
return;
}
}
}
The program allows the user to enter the name once, but the second time around, after entering the number I get the error Bus Error: 10. I've been debugging for a few hours here and I've narrowed it down to something having to do with how the number is stored. I'm also not completely sure I understand the function of free(), because when I attempt to free(temp) and them printf("%s",temp->name) right after free(temp), it still prints whatever name had been stored in name originally.
Help please!
Thanks in advance!
Khaaliq
Last edited by Corona688; 02-22-2013 at 01:05 PM..
This may belong in the C Programming forum, but here goes anyway...
What would cause a bus error? I searched google for a cause, but came up with some conflicting reports...
Could it be caused by disk space?
A lot of the pages I found mentioned linking with the incorrect versions of the... (4 Replies)
Hi there
I am running soalris 9 on a sun fire 480r and all of a sudden (today) whenever the users run the command `top` we get the following message `bus error`
does anybody have any information on what this is all about and whether there is a routine i can perform to gather more... (3 Replies)
aix 5.3 ML1
system was functioing well, when suddenly telnet session hung and then I got the following message "Bus error(coredump)"
would appreciate if anyone would assist as to what to do next. (3 Replies)
Hi,
I am using HP-UX.
While i try to run the Sqlplus command using the shel script in encounter the following error:
<shell script name>: some number Bus Error(coredump)
What may be the reason behind this.
I read few previous threads in this forum where i found some similar case. There it was... (2 Replies)
Hi!
I've got a program which runs fine under Linux, but I have compiled it to run under SunOS 5.8 in a Sparc computer, and now it sometimes fails with "bus error".
Ussing gdb I surfed to the error line, which is
*pointer = some_vector;
where some_vector is a 16 byte struct (4 integers)... (1 Reply)
Hi all,
I am getting bus error problem in SunOS. Can you please help me out in this regard.
Actually, my entire code till the last line has been executed. But after tht i am getting a bus error. Please help me.
Thanks in advance.
Charu. (4 Replies)
Hi all,
I am quite weak in C but I need to get some work done. Hope someone can help me out. I keep getting this error when i try to run my C application in my HP-UX 10.20 machine.
Some code snippet:
Month(DBTime) =====This is a function which will return variable "CutOffTime" to be use... (5 Replies)
I am getting bus error when i include "#!/bin/ksh". If i remove interpreter then script is working. Can anyone explain this and how can i avoid this error?
Operating System is HP-UX B.11.23 U 9000/800 1091834454 (2 Replies)
Hi everyone,
I have a GUI project and when I run it and left in idle state for a long time(there is nothing done, just opened GUI, no more actions),I get bus error after trying to do anything with it.
I've tried to build it in debug mode and use gdb, but I don't get any error in debug mode.It... (3 Replies)
Hi, I am new to C++ and unix. I am trying to write a programm for inserting elements into a binary tree. To get the code flow I used few Couts and m facing buss error while insertion.
Below is the code snippet. explainations needed. thanks :)
#include <iostream.h>
struct mytree
{
int... (1 Reply)