Golf Names and Scores

I am attempting to create a program that does the following;
Gets names and scores for up to 24 golfers from user.
Full name (first and last separated by space).
Validates user entries for golf scores (no values less than 42).
Uses a sentinel value to permit fewer than 24 entries.
After entries, program should display a list of golfer's names and corresponding scores (sorted from highest to lowest).

So far I have the following code;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes.
void getNames();
void getScores();
void bubbleSort(int[], int);
void swap(int&, int&);

// Array for names.
string golferNames[24];
// Array for scores.
double scores[24];

int main()
{
    string golferNames[24];
    for (int x = 0; x < 24; x++)
    {
        cout << "Please enter a Golfer's first and last name separated by a space: " << endl;
        cin >> golferNames[x];
    }

    return 0;










}
//function definitions
void bubbleSort(int values[], int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            if (values[i] > values[j])
                swap(values[i], values[j]);
        }
    }
}

void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

void getNames()
{

}

void getScores()
{

}


I am brand new to programming and c++ and am having a lot of difficulty with this program. Any advice would be greatly appreciated!
Last edited on
You have a number of problems.

Line 12,18: You have two arrays named golferNames. One global. One in main. Which one did you want to use? Generally globals should be avoided.

Line 22: cin >> stops at the first space. As jonnin pointed out in your other thread, if the user enters first name and last name, you will get only the first name. Use
 
    getline (cin,golferName[x].

Line 22: You also have a empty getNames() function, but you're getting the names here.
Nowhere do you ask for the scores. Be careful mixing cin >> and getline(). cin >> stops at the first whitespace. getline() stops at '\n'.

Line 29: You sort only sorts integers. When you do a swap, you need to swap both the golferName and the score.

Line 29: You have no check for a sentinel.

Lines 36-46: C++ provides a sort() function.

Lines 48-53: C:: provides a swap() function. Of course, if you use the built-in sort() function, you don't need your own swap() function.

Have you learned structs? You should really place name and score in a struct.
1
2
3
4
5
struct golfers_t
{
	string	name;
	int		score;
} golfers[24];


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.




Last edited on
This needs a couple of fixes.
1) Needs to sort from highest to lowest.
2) Need to range check the scores.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct golfers_t
{
	string	name;
	int		score;
};

// Function prototypes.
int get_golfers(golfers_t golfers[]);
void bubbleSort(golfers_t golfers[], int);
void swap(golfers_t & a, golfers_t & b);
void print_golfers(golfers_t[], int size);

int main()
{
	golfers_t	golfers[24];
	int			num_golfers;

	num_golfers = get_golfers(golfers);
	bubbleSort(golfers, num_golfers);
	print_golfers(golfers, num_golfers);
	return 0;
}

//function definitions
void bubbleSort(golfers_t golfers[], int size)
{
	for (int i = 0; i < size; i++)
	{
		for (int j = i + 1; j < size; j++)
		{
			if (golfers[i].name > golfers[j].name)
				swap(golfers[i], golfers[j]);
		}
	}
}

void swap(golfers_t & a, golfers_t & b)
{
	golfers_t temp = a;
	a = b;
	b = temp;
}

int get_golfers(golfers_t golfers[])
{
	int num_golfers = 0;

	for (int x = 0; x < 24; x++)
	{
		cout << "Please enter first and last name for golfers number " << x + 1 << endl;
		cout << "Enter x to exit: ";
		getline(cin, golfers[x].name);		
		if (golfers[x].name == "x")
			return num_golfers;		
		cout << "Enter score for golfer number " << x + 1 << ": ";
		cin >> golfers[x].score;
		cin.ignore(100, '\n');
		num_golfers++;
	}
	return num_golfers;
}

void print_golfers(golfers_t golfers[], int size)
{
	for (int i = 0; i < size; i++)
		{	cout << golfers[i].name << "\t" << golfers[i].score << endl;
		}
}


Please enter first and last name for golfers number 1
Enter x to exit: Fred Flintstone
Enter score for golfer number 1: 90
Please enter first and last name for golfers number 2
Enter x to exit: Barney Rubble
Enter score for golfer number 2: 86
Please enter first and last name for golfers number 3
Enter x to exit: x
Barney Rubble 86
Fred Flintstone 90


Last edited on
We have not yet covered structs but this is incredibly helpful. I will be messing with this for the next few days to try to learn it. I just have a few follow up questions;

If you could not use the struct, what would it look like?

How could I implement a method of validation so that no value below 42 is accepted for the golfers' scores?

Is the aforementioned "sentinel value" the value "x" as it is the value that may be entered to exit the program?


Sorry for the jumbled mess in my original post and thanks for the help!
If you could not use the struct, what would it look like?

a bunch of arrays. if they are different types, you would also have different sort and swap functions if you can't use c++ ones. This idea is called parallel arrays, where location [3] for example is the same 'record' ... names[3] and scores[3] and whatevers[3] are the values for the same person, bound together by the array index. This is important: if you sort by name, you have to swap all the arrays the same way so [location] continues to match for the 'record'. If you sort again by score, same thing, the names and whatever else has to be kept in sync.

a typical simple validation can just be a little loop:
do
ask for a value
get value
if value not in range (if statement(s) ) complain
while(not in range)

a sentinel is just a value that you don't expect to see in your data (eg, score = -999) that you can use to mark things. for example if you allow up to 20 values and they put in 5, the rest of the array can be marked with the sentinel value you chose to indicate the location is not used.

Last edited on
Outstanding. Thank you!
Topic archived. No new replies allowed.