How can I fix my add method?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tam13
    New Member
    • Jul 2010
    • 9

    #1

    How can I fix my add method?

    My add method gets stuck after the user inputs a name. Can anyone help me fix it please.
    Code:
    void add(void)                        //FUNCTION TO ADD A RECORD IN LIST 
    { 
     int i,j,k; 
     char cho,ch; 
     k=1; 
     do 
    	 { 
    		 temp=(struct Record *)(malloc(sizeof(struct Record)));        //DYNAMIC MEMORY ALLOCATION 
    		 printf("Enter Telephone Number : "); 
    		 scanf("%ld",&temp->telNo); 
    		 ch=getchar(); 
    		 printf("Enter Name : ");  
    		 j=0;  
    		 ch=getchar();  
    		 while(ch!='n')  
    			 {  
    				 temp->name[j++]=ch;  
    				 ch=getchar();  
    				 }  
    		 temp->name[j]=' ';  
    		 printf(" Enter Record's Address:: ");  
    		 scanf("%s",temp->address);  
    		 printf("\n Enter Record's Year of Birth. :: ");  
    		 scanf("%d",&temp->yearOfBirth);  
    		 ch=getchar();  
    		 printf("\n Do You Want to Save This Record (y/n)??");  
    		 cho=getchar();  
    		 if((cho=='n')||(cho=='N'))  
    			 {  
    				 free(temp);                //IF NOT TO SAVE THEN FREE THE ALLOCATED MEMORY  
    				 goto label;  
    				 }  
    		 temp->next=top;  
    		 top=temp;  
    		 printf(" One  Record Added");  
    		 label:  
    		 ch=getchar();  
    		 printf(" Enter Another Record (y/n) ::");  
    		 cho=getchar();  
    		 }while((cho=='y')||(cho=='Y'));  
     }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    How is Record::name (or the whole Record) declared?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This code:

      Code:
      while(ch!='n')
      is used to enter a name. Do you mean that names can't contain an n or do you mean:

      Code:
       while(ch!='\n')
      to receive characters until you reach the enter key pressed by the user.

      Comment

      • tam13
        New Member
        • Jul 2010
        • 9

        #4
        Here is how record is declared:

        Code:
        struct Record                           
        {  
         long int telNo;  
         int yearOfBirth;  
         char name[40],address[40];  
         struct Record *next;                  
         }rec1,rec2;                        
        struct Record *temp,*previous,*current;         
        struct Record *top=NULL;

        Comment

        • tam13
          New Member
          • Jul 2010
          • 9

          #5
          Code:
          while(ch!='\n');
          That is what I meant to put. However when I make the change it suddenly skips over name to address. Then when I enter address it goes into an infinite loop.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Probably your are fetching the \n from the telephone number. Most likely you parsed out the telephone number and left the \n in the input stream.

            You might do a getchar() before you start parsing the name to eat that leftover \n.

            Comment

            • tam13
              New Member
              • Jul 2010
              • 9

              #7
              After which line in the code should I do the getchar()?

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Here is a piece of your code:

                Code:
                ch=getchar();  
                         printf("Enter Name : ");   
                         j=0; 
                		 
                         ch=getchar();   
                         while(ch!='\n')   
                             {   
                                 temp->name[j++]=ch;   
                                 ch=getchar();   
                                 }   
                         temp->name[j]='\0';   
                         printf(" Enter Record's Address:: ")
                The first getchar() fetches the enter key from the telephone number.
                The second getchr() fetches the first character of the name.
                The third getchar() inside the loop fetches the rest of the characters in the name. This will include the \n that breaks thge loop.
                Then I changed you code to temp->name[j]='\0'; to put a null terminator on the name to make it a string. Your original code appended a space and I didn't thisnk that was correct.

                Based on cursory debugging using Visual Studio.NET 2008, you code appears to work as far as obtaining data is concerned.

                You might consider using fgets() to fetch you name since a lot of code in your program is the guts of fgets().

                Comment

                Working...