Get name and number in one line

Feb 25, 2013 at 12:35am
Hey there,
I'm writing a program that takes 5 players names and scores and sorts them and outputs the top 3.
I just started writing this and already I'm stuck. How can I get someone's name(I'm going to limit them to first name only) and their score, both in one line and save each of those values for future sorting?
Please and thank you
Feb 25, 2013 at 12:45am
Store them in a struct? And output is pretty easy.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

struct players
{
	std::string name;
	int score;
};

int main()
{
	// Create 5 players
	players plyr[ 5 ];

	plyr[ 0 ].name = "Dave";
	plyr[ 0 ].score = 187;

	// ... //

	std::cout << plyr[ 0 ].name << " " << plyr[ 0 ].score << '\n';       

	return 0;
}


Output:
Dave 187
Last edited on Feb 25, 2013 at 12:46am
Feb 25, 2013 at 1:34am
Thanks man, I'll look more into this, but will it still work if I get the names and scores as input from the user?
Feb 25, 2013 at 2:03am
Sure
1
2
	cout << "Enter name: "; cin >> plyr[ 0 ].name;
	cout << "Enter score: "; cin >> plyr[ 0 ].score;
Feb 25, 2013 at 12:22pm
Yeah, sorry about the input. I just used hardcoded data as an example. (:
Topic archived. No new replies allowed.