Thread: Problem with printing arrays

  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
    28,413
    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;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 05: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