How is Unique declared?
--
Mats
How is Unique declared?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:string Unique [BOOK_MAX];
So, can you post the whole function that this is in?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Uhh, all my code is int main. I'm still not very good with functions and due to deadlines I decided to just do it the way I can. Here's the whole case, if that helps any:
Code:int select; cin >> select; switch (select) { case 1: { int flag = 0; int count = 0; for (int i = 0; i < (BOOK_MAX); ++i) { if (Books[i].Author == "") { } else { Sort[count] = Books[i].Author; ++count; } } sort(Sort, Sort+count); int uniquecount = 0; for (int x = 0; x < count; ++x) { if (Sort[x] == Sort[x+1]) { } else { Unique[uniquecount] = Sort[x]; ++uniquecount; } } /*for (int q = 0; q < uniquecount; ++q) { for (int w = 0; w < BOOK_MAX; ++w) { if (Unique[q] == Books[w].Author) { cout << Books[w].Title << " in section " << Books[w].Category; for (int e = 0; e < BOOK_MAX; ++e) { if (Book[w].ID == Loans[e].Book) { cout << " - on Loan." << endl; } else { cout << " - in Stock." << endl; } } } } } */ cout << Unique[1] << Unique[2] << endl; break; }
And about 5 lines around the declaration of Unique...
--
mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Code:Book Books [BOOK_MAX]; User Users [USER_MAX]; Loan Loans [BOOK_MAX]; string Sort [BOOK_MAX]; string Unique [BOOK_MAX]; int main()
I have a feeling that this is a case of "more haste less speed".Originally Posted by Furious5k
You could start by turning the block of code following case 1 into a function. Give it a descriptive name, add a comment that describes what the function is supposed to do. It looks like you made the mistake of using global variables unnecessarily, but nevermind, for the function practice using arguments. You would find that when you focus on a small piece of code without distractions (thanks to local scope), reasoning about your program and figuring out what is wrong becomes easier.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
If you're allowed to use the standard library for sorting, perhaps you're also allowed to use it for removing duplicates as well?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
see anything wrong? If not, look closer!Code:if (Book[w].ID == Loans[e].Book)
How about using the != operator instead of == operator?Code:if (Sort[x] == Sort[x+1]) { } else { Unique[uniquecount] = Sort[x]; ++uniquecount; }
And x+1 still goes outside of the array.
Yes, I agree with Laserlight - your case 1: should definitely be it's own function.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Why don't you just overload operator< for your Books Class and call sort() directly on your class.
Then just delete the empty arrays. Of course I am usually wrong.Code:bool Books::operator<(const Books obj) const { if(Author < obj.Author) return true; else if (Author == obj.Author) return true; else return false; }
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I agree with the passing by reference, but if you have books of the same author wouldn't they be equal? i'm not too sure.
EDIT:
Well wouldn't you want them to be treated equal by the sort() algorithm, you can also add nested ifs to the overload function to include the other members of the class so that the sort() would be more specific than just sorting the authors. Just me pondering.
Last edited by Phyxashun; 01-06-2009 at 01:51 PM.
If they are "equal", then one is not "less than" the other, so you should return false.Originally Posted by Phyxashun
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Ah, more failed logic on my part. I agree
EDIT:
And if you wanted to move the empty class members to the end couldn't you change the overloaded operator< to make it >
Code:bool Books::operator<(const Book &obj) const { if (Author > obj.Author) return true; return false; }
Last edited by Phyxashun; 01-06-2009 at 01:58 PM.
That would result in a reverse ordered sort. More accurately:Originally Posted by Phyxashun
Code:bool Books::operator<(const Book &obj) const { if (Author.empty()) { return !obj.Author.empty(); } else if (obj.Author.empty()) { return false; } return Author < obj.Author; }
Last edited by laserlight; 01-06-2009 at 02:10 PM. Reason: Fix for the case where the RHS author is empty.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)