please help me with this code... arrays arent working.

This is a discussion on please help me with this code... arrays arent working. within the C Programming forums, part of the General Programming Boards category; I'm working on a food bank software for a c prog. class. the program has a list of five choices ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    66

    please help me with this code... arrays arent working.

    I'm working on a food bank software for a c prog. class.
    the program has a list of five choices when in runs.
    1-make a donation
    2-make a request
    3-fill a request
    4-print status report
    5-exit

    when the user inputs 1 for make a donation, they are asked to input the inventory type and amount. this all works fine. i added a test at the end of the donation section of the code that prints the inv. type and amount and this is working fine.
    when i print the status report after making 2 donations, it only prints the last donation. as another check i had it print its position in the array. and it says position (0). im lost. im guessing the error is somewhere in the donations section but i cant figure it out. if i change anything, i get different errors.

    because this is an assignment i am only going to post a section of the code. if you think you can help i can PM or email you the rest of it. or maybe you'll be able to see whats wrong without the rest of the code.

    thanks in advance for the help.

    Code:
    code removed

    just to explain the program a little more.
    this is my first programming course so that is why my code is so elementary. (but im trying!)
    if the user inputs a second donation of the same inventory type it should just add the amount of the first donation and the second donation without making another entry in the inventory type. just in case you are wondering why there is a strcmp in there.
    Last edited by ominub; 02-24-2009 at 08:19 PM. Reason: removed code

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You spelled dontation wrong.

    What are you trying to do here:
    Code:
    for (i=0; i<=d; i++) 
                        {
                            if (strcmp(string, don_inv_type[i])==0)
                                { 
                                    i++;                           
                                }
                        }
    Besides, as you say, skipping a step if the string matches. Because all it does is skip a bunch of steps anyway, and the only effect will be to leave i==d in all cases. Which you don't appear to use i for anything anyway, and it is guarenteed to equal d. So you really need to explain what the loop was supposed to be for.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    well i figured it out anyway. i was incrementing it all wrong. i had i++ before anything was stored so it automatically skipped to the next slot in the array. at least i think thats what was wrong. i just re arranged some stuff and it worked.

    thanks anyway.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Anyway -- just don't be using a loop like that again.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Code Is Not Working
    By srivatsan in forum C++ Programming
    Replies: 23
    Last Post: 11-12-2008, 12:30 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. um canīt understand why this code isnīt working...
    By Shogun in forum C Programming
    Replies: 9
    Last Post: 04-25-2003, 10:38 AM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 02:26 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 04:04 PM

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