How is Unique declared?
--
Mats
Printable View
How is Unique declared?
--
Mats
Code:string Unique [BOOK_MAX];
So, can you post the whole function that this is in?
--
Mats
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
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".Quote:
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.
If you're allowed to use the standard library for sorting, perhaps you're also allowed to use it for removing duplicates as well?
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
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;
}
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.
If they are "equal", then one is not "less than" the other, so you should return false.Quote:
Originally Posted by Phyxashun
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;
}
That would result in a reverse ordered sort. More accurately:Quote:
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;
}