Problem with printing arrays

This is a discussion on Problem with printing arrays within the C Programming forums, part of the General Programming Boards category; Success!!! Thank you very much been trying to get that sorted for a couple hours now :P Now I can ...

  1. #16
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Success!!!

    Thank you very much

    been trying to get that sorted for a couple hours now :P

    Now I can move onto my second program, making a penalty maze out of multi-dimensional arrays... expect cries for help in a new thread shortly lol :P

  2. #17
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Quote Originally Posted by laserlight View Post
    On second thought, it should be more like:
    Code:
    while (fscanf(input, "%i%29s%i%i", &info.week, info.name, &info.units, &info.price) == 4)
    {
        week[info.week] = info.total = info.units * info.price;
        if (info.total > largest.total)
        {
            largest = info;
        }
    }
    The += thing kind of tricked me: you're just adding to 0, but your intention is really to assign, not to add to the current value.
    Using += allowed it to find the total
    I originally tried
    Code:
    info.total = info.units * info.price
    week[info.week] = info.total
    However, it just kept feeding back the wrong info, the data file it refers to has different information stored about the same week more than once, and the objective is to print out the total for each week, so I add them all together, but because they arnt in any order I have to use info.week so it adds the values to the correct week, if that makes sense.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    In that case, that loop really should just be this:
    Code:
    while (fscanf(input, "%i%29s%i%i", &info.week, info.name, &info.units, &info.price) == 4)
    {
        week[info.week] += info.units * info.price;
        info.total = week[info.week];
        if (info.total > largest.total)
        {
            largest = info;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Thats what it is well with the exception of my while loop runs while != EOF but otherwise its exactly the same, I changed it when you first posted and from then it worked.

    Thanks for the help

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    Quote Originally Posted by anxty
    well with the exception of my while loop runs while != EOF but otherwise its exactly the same, I changed it when you first posted and from then it worked.
    Checking against the number of format specifiers is usually better in case the input is not well formed (though your results will arguably still be bogus). Notice also that I used %29s instead of %30s to account for the null character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Each file varies in size, which is why I need to go by EOF

    and i thought if you had an array e.g. char name[30] 0-29 were used as character slots and 30 was the null character, and therefore using %30 would allow for 30 characters (0-29?) or have I got that wrong and it uses 0-28 and 29 as null character :P

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    Quote Originally Posted by anxty
    Each file varies in size, which is why I need to go by EOF
    The files may vary in size, but they should have complete sets of information. For example, keep to your current code and say, remove the last price from the file. You will probably find that everything works as per normal, except that the last read keeps the price from the second last read.

    Quote Originally Posted by anxty
    and i thought if you had an array e.g. char name[30] 0-29 were used as character slots and 30 was the null character, and therefore using %30 would allow for 30 characters (0-29?) or have I got that wrong and it uses 0-28 and 29 as null character :P
    You got that wrong

    In general, if you declare an array to have N elements, there will not be an element at index N.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to arrays problem
    By key4life in forum C Programming
    Replies: 11
    Last Post: 02-13-2010, 04:56 PM
  2. Problem with Arrays
    By dldsob in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2009, 08:43 PM
  3. Problem with printing instruction pointer
    By Subsonics in forum C Programming
    Replies: 0
    Last Post: 10-11-2009, 03:33 PM
  4. Printing 2d array problem
    By Rob4226 in forum C Programming
    Replies: 3
    Last Post: 10-01-2009, 03:56 PM
  5. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM

Tags for this Thread


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