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
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
Using += allowed it to find the total
I originally tried
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.Code:info.total = info.units * info.price week[info.week] = info.total
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; } }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.Originally Posted by anxty
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.Originally Posted by anxty
You got that wrongOriginally Posted by anxty
In general, if you declare an array to have N elements, there will not be an element at index N.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)