printing issue

This is a discussion on printing issue within the C Programming forums, part of the General Programming Boards category; Hi Please advise, I have 2 function (AddEntry & PrintEntry) I don't understand why the "PrintEntry" function doesn't print the ...

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

    Question printing issue

    Hi

    Please advise,
    I have 2 function (AddEntry & PrintEntry)
    I don't understand why the "PrintEntry" function doesn't print the first element at "phonebook[x].Name".

    Code:
    int counter=0;
    void AddEntry(phone phonebook[])
    {
    
        counter++;
    
        printf("Enter first name:\n");
        scanf("%s",phonebook[counter-1].Name);
        printf("Enter last name:\n");
        scanf("%s",phonebook[counter-1].Suraname);
        printf("Enter phone number:\n");
        scanf("%s",phonebook[counter-1].PhoneNumber);
    
    }
    
    void PrintEntry(phone phonebook[])
    {
        int x;
        for(x=0;x<counter;x++)
        {
         printf("Name=%s :: Lsat name=%s  :: PhoneNumber=%s\n",phonebook[x].Name ,phonebook[x].Suraname,phonebook[x].PhoneNumber);
        }
    
    }
    Code:
    int main(void)
    {
    phone phonebook[20];
    int i;
            for (i=0;i<4;i++)
           {
            AddEntry(phonebook);
            PrintEntry(phonebook);
    }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    No idea - it works for me.
    Code:
    #include <stdio.h>
    typedef struct {
      char Name[100];
      char Suraname[100];
      char PhoneNumber[100];
    } phone;
    
    int counter=0;
    void AddEntry(phone phonebook[])
    {
    
        counter++;
    
        printf("Enter first name:\n");
        scanf("%s",phonebook[counter-1].Name);
        printf("Enter last name:\n");
        scanf("%s",phonebook[counter-1].Suraname);
        printf("Enter phone number:\n");
        scanf("%s",phonebook[counter-1].PhoneNumber);
    
    }
    
    void PrintEntry(phone phonebook[])
    {
        int x;
        for(x=0;x<counter;x++)
        {
         printf("Name=%s :: Lsat name=%s  :: PhoneNumber=%s\n",phonebook[x].Name ,phonebook[x].Suraname,phonebook[x].PhoneNumber);
        }
    
    }
    int main(void)
    {
    phone phonebook[20];
    int i;
            for (i=0;i<4;i++)
           {
            AddEntry(phonebook);
            PrintEntry(phonebook);
    }
    return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    Enter first name:
    fred
    Enter last name:
    flintstone
    Enter phone number:
    12345
    Name=fred :: Lsat name=flintstone  :: PhoneNumber=12345
    Enter first name:
    barney
    Enter last name:
    rubble
    Enter phone number:
    321
    Name=fred :: Lsat name=flintstone  :: PhoneNumber=12345
    Name=barney :: Lsat name=rubble  :: PhoneNumber=321
    Enter first name:
    wilma
    Enter last name:
    flintstone
    Enter phone number:
    1245
    Name=fred :: Lsat name=flintstone  :: PhoneNumber=12345
    Name=barney :: Lsat name=rubble  :: PhoneNumber=321
    Name=wilma :: Lsat name=flintstone  :: PhoneNumber=1245
    Enter first name:
    betty
    Enter last name:
    rubble
    Enter phone number:
    245
    Name=fred :: Lsat name=flintstone  :: PhoneNumber=12345
    Name=barney :: Lsat name=rubble  :: PhoneNumber=321
    Name=wilma :: Lsat name=flintstone  :: PhoneNumber=1245
    Name=betty :: Lsat name=rubble  :: PhoneNumber=245
    Perhaps in your attempt to be brief, you've managed to edit out the actual bug.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    58
    I work with eclipse on linux,I think the problem is related to eclipse.
    I'll try to run it on command line.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    > I don't understand why the "PrintEntry" function doesn't print the first element at "phonebook[x].Name".
    Perhaps it scrolled of the top of your console window?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange issue with printing array value
    By CodeKate in forum C Programming
    Replies: 6
    Last Post: 11-24-2010, 04:52 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-06-2010, 12:52 AM
  3. Issue with printing a varying array
    By monki000 in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2010, 05:06 PM
  4. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-04-2008, 12:12 AM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 05:02 PM

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