address book

This is a discussion on address book within the C Programming forums, part of the General Programming Boards category; I wrote an address book program with everthing functioning well. When I choose the print_entry option, it is supposed to ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    21

    address book

    I wrote an address book program with everthing functioning well. When I choose the print_entry option, it is supposed to print out all 3 entries that i put in, however it only prints out the most recent entry. Any advice on getting this right?

    Code:
               case 2:
               
               printf("%d", TotalAddress);
               for (x=0; x <= TotalAddress; x++)
               {
               print_entry(new_info[x]);
               }
               break;
    Last edited by cboard; 04-06-2007 at 12:08 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your for loop in the main for case 2 should start at 0 right? also 'x' should be initialized to 0 instead of 1, i would imagine

    edit: just ran the program and added 3 entries and it printed all 3 fine.. it seg faulted when i exited though
    Last edited by nadroj; 04-05-2007 at 09:29 PM.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Quote Originally Posted by nadroj View Post
    your for loop in the main for case 2 should start at 0 right? also 'x' should be initialized to 0 instead of 1, i would imagine

    edit: just ran the program and added 3 entries and it printed all 3 fine.. it seg faulted when i exited though
    really? Im using DevC++ and its not working for me...what program are you using to run it if you dont mind me asking? also what does seg fault mean?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im on linux, using GNU C compiler, but that shouldnt make a difference.

    in your case 2 in main, change the condition to just < (not <=). it still prints fine and there was no segfault this time.

    im sure you know this since its your program, but make sure your entering what you should, since your reading all those variables (ie for address) at once (not user-friendly, i had to look at the code to know what to type).

    edit: check this to learn about segmentation fault: http://en.wikipedia.org/wiki/Segmentation_Fault

    edit2: the last 2 statements in main (system..return..) wont ever get executed so you can remove them.

    what is numb2 for? you dont use it for anything besides your condition for the do loop, remove it and just put while(1), since while(numb2) will always be true because numb2 is always set (at the beginning of the do loop) to a non-zero value (meaning true)
    Last edited by nadroj; 04-05-2007 at 10:10 PM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Quote Originally Posted by nadroj View Post
    im on linux, using GNU C compiler, but that shouldnt make a difference.

    in your case 2 in main, change the condition to just < (not <=). it still prints fine and there was no segfault this time.

    im sure you know this since its your program, but make sure your entering what you should, since your reading all those variables (ie for address) at once.
    I made all the changes you suggested. When I come to print the menu after making 3 entries, it will print out a bunch or random characters for the first 2 entries. I just need this to work correctly on Microsoft Visual Studio, but I dont have a copy of it on my computer so Im using DevC++. You think it will work just fine on that?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok i tried again and im getting the same behaviour, only the last person is printed properly. i recommend asking the user for input each time (instead of reading 3 strings for name, ask for each part-- title, then first, then last), same with address.

    i did a quick edit and only scanned and printed the first name, zip, and age, and it printed fine for all 3 entries. this makes me think theres a problem with your input--would be best to read only one at a time
    Last edited by nadroj; 04-05-2007 at 10:31 PM.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    ah i see, thanks for looking into that for me. They want the input to be in this format, it makes a bit more complicated i agree. Ill double check the inputs and see if I find anything off

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i think it should still work fine, as long as you enter everything _exactly_ as you should.

    i just tried the edited code you have above again (however changing the for condition in the main case 2 to '<') and it printed all 3 entries fine. you have to be sure your typing exactly what its expecting.

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    yeah your probably right, i absolutely see no reason as to why it shouldn't be working. Oh and i changed the condition in case 2

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    have you tried with valid input and does it print fine

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > entry add_entry(entry new_info)
    The entry you pass into the function serves no purpose. You may as well have said
    Code:
    entry add_entry ( void ) {
        entry new_info;
    > new_info[x] = add_entry(new_info[x]);
    What increments x when you input?

    It's better to use your actual counter
    new_info[TotalAddress] = add_entry();
    TotalAddress++;

    Also, printing out oversteps the array, so
    for (x=0; x <= TotalAddress; x++)
    should be
    for (x=0; x < TotalAddress; x++)
    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. I am very new . . . :(
    By Eternalglory47 in forum C++ Programming
    Replies: 6
    Last Post: 09-05-2008, 11:29 AM
  2. Replies: 10
    Last Post: 09-04-2008, 01:27 PM
  3. simple structure/linked list question
    By niponki in forum C Programming
    Replies: 16
    Last Post: 08-14-2005, 11:13 PM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  5. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM

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