"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
very helpfull, been trying for a while with that. No matter what i change it still keeps on crashing. And because i´m fairly new, i can´t figure out why because i get no errors.
What do you have problems with. The signature of qsort is this:
void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
First param: first elem of the array => items or &items[0]
Second param: number of array elements => NUM_ITEMS
Third param: size of an array element => sizeof(items[0] or sizeof(struct varam)
Forth param: the compare function => nummer_sortering
When printing the sorted elements they get sorted, but the diffrent elements get mixed up and wrong.
They should be printed like this:
nummer: 1575
namn: Anchor Steam Beer
pris: 22.900000
volym: 355.000000
typ: Mellanm├Ârk lager
stil: California common
forpackning: Flaska
land: USA
producent: Anchor Brewing Company
alkoholhalt: 4.800000
but get printed all messed up like this :
pris: 0.000000
volym: 6099781802703388100000000000000000.000000
typ:
stil:
forpackning:
land: @
producent:
alkoholhalt: 0.000000
I guess I am missing something
code:
Code:int nummer_sortering(const void* n1, const void* n2) { const struct vara *item1 = n1; const struct vara *item2 = n2; return item1->nummer - item2->nummer; } printf("\n\nVaror sorterade på varunummer:\n"); qsort(items, NUM_ITEMS, sizeof(items[0]), nummer_sortering); for (i = 0; i < NUM_ITEMS; i++) { printf("nummer: %d\n" "namn: %s\n" "pris: %f\n" "volym: %f\n" "typ: %s\n" "stil: %s\n" "forpackning: %s\n" "land: %s\n" "producent: %s\n" "alkoholhalt: %f\n\n", items[i].nummer, items[i].namn, items[i].pris, items[i].volym, items[i].typ, items[i].stil, items[i].forpackning, items[i].land, items[i].producent, items[i].alkoholhalt ); free(oneline); } } fclose(fp); }
As I stated before NUM_ITEMS is likely wrong!
You only want to sort the valid data in the array; do not sort the empty/garbage elements.
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
I think you misunderstood Tim.
The calculation of NUM_ITEMS is correct: the size of the entire array divided by the size of a single element.
What Tim is saying is that NUM_ITEMS is not the thing you should pass to qsort (unless, of course, your array is completely full).
You have an array that can hold 100 items. But if you only happen to have 42 valid entries in your array, the other 58 entries are still there, they're just full of garbage. When you sort, those garbage items get mixed in with your real data, which is likely what causes your bogus output.
What you need is some sort of variable to track how many valid items are in your array, and only sort that many. if only you had a variable that counted the number of items you read in from your file.
Here is the link to my question!(c - qsorting a struct with arrays, error as output - Stack Overflow) I guess you are saying that I have the variable in my code aldready haha, Can you give me a clue? I am really bad at this sorry
Last edited by emano12; 01-31-2018 at 07:09 AM. Reason: link
I wonder if that bolded i was a clue... if only there was some clue about the clue! Oh no!
I tried "i" haha, but it didnt work out. Sneaky though
I do not understand why the output is all messy. I have explained the question better on Stackoverflow
c - qsorting a struct with arrays, error as output - Stack Overflow
What does that mean, it didn't work out? If it didn't work out you make it work out. Maybe make listanr() return its i, and then use that number for this purpose.
No reason to just deny the answer staring us right in our faces.
I did not mean it in a cruel way. Im so sorry! I am just a little frustrated with programming, because I have not got the hang of it. I really appreciate you helping me, do not think otherwise. Could you be an angel and check out the link? Then you will understand my "anger" about the output
I did, the link is as bad as the explanation here.Could you be an angel and check out the link?
Frankly, no one can compile your code, you've neglected to include an entire program at any point. Context that you've deemed unimportant is absent, like where main() is, what it contains, and even how many functions there might be. Thus, it is difficult to even explain things that we see at a glance. It's unsurprising that "i" didn't work when you tried it because I'm sure the actual code looks waaaaay different from what you thought you should post.
Read this for next time: How to create a Minimal, Complete, and Verifiable example - Help Center - Stack Overflow
At this point, starting over with a good example is as troublesome as not doing that, so I'll just tell you.
Do you remember where this loop is in your code? Go find it. Well, when this loop finishes, you know exactly how many beers are in the array. It's i. You need to save, and use, this number for sorting. Whatever else that might entail, like returning it from one function and passing it to several others, well, you've made that your problem.Code:for (i = 0; i < 100 && fgets(envara, 512, fp); i++)