![]() |
|
#1
|
|||
|
|||
Sorting (structures and arrays fun fun..)k second semester coding... and i get this lab, its basically done.. but the prof gave us this code to sort our structure or array or w/e
this is C, and i can only use C CPP / C++ / C Code:
obviously this is for arrays, so were suppose to change it so itll sort the our arrays according to the name part of the structure.. youll know what i mean.. CPP / C++ / C Code:
comments moved when i sent it to myself but w/e lol and to clearify, i need to basically just use the first part of coding. i did (changed name to sort), but it doesn't work so i need to know what i need to fix so it actually does sort. btw as is, it gives me 3 error C2106s ( '=' left operand must be 1-value), 1 at each of the places i say array[w/e].name=array[w/e].name thanks for reading :O! |
|||
|
#2
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)Quote:
As far as your specific error messages: That element of the struct is an array. In C (C++. too) you can't copy an array with an assignment statement. I will remind you that the strcpy() function might be appropriate since the arrays hold C-style "strings." See Footnote. As far as functionality: I will also remind you that you can't compare contents of an array with another by comparing the names of the arrays. You might try strcmp() in places whether you want to see whether one "name" is greater than another. Regards, Dave Footnote: The name of an array, used by itself (without the [] brackets) is taken to be a constant pointer to the first element of the array. That's why it is an error to put the name of an array on the left side of an assignment statement. That's also why it is not productive to compare the names of the arrays when you want to compare the contents of the arrays. |
|
#3
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)well lets assume the rest of the program runs peachy and stuff, and i have tried string commands on it, unless i was just a turdbuglar and messed it up
anyways you mean somin like... CPP / C++ / C Code:
its printing this now (#s representing their position from top to bottom within the input text file) name1 1 1 1 1 name1 2 2 2 2 name3 3 3 3 3 name3 4 4 4 4 name5 5 5 5 5 name5 6 6 6 6 any ideas =/? |
|
#4
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)Quote:
Huh? What's printing what? The second program in your original post didn't compile, and had (at least) one other kind of bug that I mentioned. The function in your most recent post is the sort program for an array of ints, and doesn't print anything. It works for my at least one test case: CPP / C++ / C Code:
Output: Code:
So, what is it that you are testing? What are you asking? Regards, Dave Footnote: Just because it appears to work with that single test case, don't take it to mean that I am certifying that it is OK. You can't prove the program works by testing, and certainly not with just one test case. |
|
#5
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)i replaced the sort function from my original actual program, with the sort i just posted because thats what i need fixed
heres what it stands at now CPP / C++ / C Code:
heres the input file: Cardozzi, Emil 85 94 79 93 Antonucci, Peter 75 91 89 89 Messier, Paul 87 88 89 90 Matsumoto, Carla 78 96 88 91 Pfizer, Lisa 83 79 93 91 Couto, Michael 73 86 92 90 So whats being spit out is..: Cardozzi, Emil 85 94 79 93 Cardozzi, Emil 75 91 89 89 Messier, Paul 87 88 89 90 Messier, Paul 78 96 88 91 Pfizer, Lisa 83 79 93 91 Pfizer, Lisa 73 86 92 90 sorry about not being crystal clear |
|
#6
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)Quote:
Well, you had already posted the integer sort. Quote:
Huh? CPP / C++ / C Code:
Where the heck did (strcmp(...,...) == 1) come from? If you want to test to see whether one string is greater than the other, wouldn't it be more like (strcmp(...,...) > 0) Regards, Dave |
|
#7
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)well if the string on the left its larger, and returns 1 as far as i know.... and if the right is bigger it returns -1.. and 0 if theyre the same.. therefore >0 would be the same as == 1
i tried >0 after seeing yer response and got the same results but lol that doesnt really matter...at this point im just throwing around different stuff till someone can be like.. "try this" =p and "heres where it stands now" is a saying... like "this is what i have now" >_>; |
|
#8
|
|||
|
|||
Re: Sorting (structures and arrays fun fun..)got it to work finally yay lol (spent more time on the lame sort the prof gave us than the program itself i bet lol.)
CPP / C++ / C Code:
so basically i needed my sort function to look like that =/ thanks for the trouble, reminding me to try the str commands again was clutch |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What is an array? | Howard_L | C Programming Language | 3 | 05-Oct-2007 05:11 |
| How Do I Create a Function that returns a usable array of pointers to structures? | UncleRic | C Programming Language | 2 | 03-Sep-2007 11:17 |
| arrays of structures as an argument? | 25cents | C++ Forum | 6 | 06-Dec-2006 15:20 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The