Comparing ints & using sstream with get functions

Hi Guys
This is my first question here, so I hope it's clear enough :)

Today I had my first exam in c++ in uni - I study computer science - and I didn't get it all right, because the time was too short and I had to write some long code to do simple tasks.
So it was a card game simulation with all kind of different methods.

First problem:
We should compare the point values of each player's card to determine the highest, which are all stored in a player class object as a vector class data element. What I did was: get the card, store the value in an int and then compare all like this:
if(a > b && a > c && a > d){..... and I had to do this four times.
Is there a better way to do this ? If not, maybe an easier way to compare the integers ?

Second problem:
If you look at this you'll probably know what the problem is

int id0 = players[0].getID();
int bd0 = players[0].getBudget();
int id1 = players[1].getID();
int bd1 = players[1].getBudget();
int id2 = players[2].getID();
int bd2 = players[2].getBudget();
int id3 = players[3].getID();
int bd3 = players[3].getBudget();

stringstream players;
players << "Player's ID" << setw(10) << "Budget" << endl;
players << "-----------" << setw(10) << "------" << endl;
players << id0 << setw(20) << bd0 << endl;
players << id1 << setw(20) << bd1 << endl;
players << id2 << setw(20) << bd2 << endl;
players << id3 << setw(20) << bd3 << endl;

return players.str();

I can't figure out why the functions above didn't work in the stringstream directly !! The compiler kept telling me that sstream doesn't support the [] operator. Even though it worked in other methods.

I hope these aren't too many questions, but I was really close to getting a perfect score, but I wasted too much time on these things, so I couldn't test my program well enough :/

Thanks a lot in advance
We should compare the point values of each player's card to determine the highest
Is there a better way to do this ?

Copy the values to a vector and use:

The second one is because you have two variables named "players".

Please use code tags around your code (format box on the right labeled "<>").
Thanks, The second issue is resolved. I changed it to this now:

        stringstream player;
	player << "Player's ID" << setw(10) << "Budget" << endl;
	player << "-----------" << setw(10) << "------" << endl;
	for(int i = 0; i < 4; i++){
		player << players[i].getID() << setw(20) << players[i].getBudget() << endl;
	return player.str();

Too bad I didn't notice it during the test :/

However, I don't think that the first problem should be solved this way cause we haven't learned anything like it during the semester. Maybe there's an easier way ?! :)
Last edited on
Topic archived. No new replies allowed.