Prompting for array values

I found the following code while searching through the forums and I have found that while it works perfectly for displaying the already set values stored in the array, how would I change the code to prompt for the users' names and prompt for their score?

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
#include <iostream>
#include <string>
using namespace std;

void displayScores(string names[], int score[], const int amountOfPlayers);
void sort(string names[], int score[], const int amountOfPlayers);

int main(void)
{
  // create variables
  const int PLAYERS = 5;
  string names[PLAYERS];
  int scores[PLAYERS];
  

  // initialize arrays
  names[0] = "John";
  names[1] = "Mary";
  names[2] = "Frank";
  names[3] = "Larry";
  names[4] = "Samantha";
  
  scores[0] = 3;
  scores[1] = 2;
  scores[2] = 5;
  scores[3] = 1;
  scores[4] = 4;

  
  // Display
  cout << "Before Sorting:\n";
  displayScores(names, scores, PLAYERS);
  sort(names, scores, PLAYERS);
  cout << "\nAfter Sorting:\n";
  displayScores(names, scores, PLAYERS);

  system("pause");
  return 0;
}

void displayScores(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers; i++)
  {
    cout << names[i] << ": " << scores[i] << endl;
  }

}

void sort(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers - 1; i++)
  {
    for (int j = i + 1; j < amountOfPlayers; j++)
    {
      if (scores[i] < scores[j])
      {
        string storeName = names[i];
        int storeScore = scores[i];
        names[i] = names[j];
        scores[i] = scores[j];
        names[j] = storeName;
        scores[j] = storeScore;
      }
    }
  }
}
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
#include <iostream>
#include <string>
using namespace std;

void displayScores(string names[], int score[], const int amountOfPlayers);
void sort(string names[], int score[], const int amountOfPlayers);

int main(void)
{
  // create variables
  const int PLAYERS = 5;
  string names[PLAYERS];
  int scores[PLAYERS];
  


  for(int i = 0; i < PLAYERS; i++)
  {
	  cout << "Name " << i+1 << ": ";
	  getline(cin, names[i]);
	  cout << "Score for " << names[i] << ": ";
	  cin >> scores[i];
	  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  }
  
  // Display
  cout << "Before Sorting:\n";
  displayScores(names, scores, PLAYERS);
  sort(names, scores, PLAYERS);
  cout << "\nAfter Sorting:\n";
  displayScores(names, scores, PLAYERS);

  system("pause");
  return 0;
}

void displayScores(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers; i++)
  {
    cout << names[i] << ": " << scores[i] << endl;
  }

}

void sort(string names[], int scores[], const int amountOfPlayers)
{
  for (int i = 0; i < amountOfPlayers - 1; i++)
  {
    for (int j = i + 1; j < amountOfPlayers; j++)
    {
      if (scores[i] < scores[j])
      {
        string storeName = names[i];
        int storeScore = scores[i];
        names[i] = names[j];
        scores[i] = scores[j];
        names[j] = storeName;
        scores[j] = storeScore;
      }
    }
  }
}


Reason for getline(), you can include name with spaces.
Last edited on
How would I add another sort in there for ascending alphabetical order by name?
Topic archived. No new replies allowed.