Trouble with Arrays

So I am trying to write a code that can take the names and scores of a certain number of people that is decided upon by the user. I then have to put it in order of highest score to lowest score which I know how to do I just do not have the code for it below. The problem I am having at the moment is that I can not get the arrays to accept user input. If I get rid of the array that is for the names then the code works fine and stores the correct number of numerical values. I just can not figure out how to get it to store names in an array repeatedly. Can somebody explain what the problem might be please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  // Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int size;
    cout << "How many records will be entered?" << endl;
    cin >> size;

    char name [size];
    float score [size];
    
    for (int i=0; i<size; i++)
    {
        cout<< "Enter the names and scores of the students." << endl;
        cin >> name [i];
        cin >> score [i];
    }   
}
closed account (E0p9LyTq)
You can't create a C-style array of variable size at run, you need a C++ container for that. A vector works well.

Creating a structure to contain a student's name and score makes it easier to deal with the data and the vector.

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
#include <iostream>
#include <string>
#include <vector>

struct student_data
{
   std::string name;
   float score;
};

int main()
{
   int size;
   std::cout << "How many records will be entered? ";
   std::cin >> size;

   student_data data;
   std::vector<student_data> students;

   for (int loop = 0; loop < size; loop++)
   {
      std::cout << "Enter the names and scores of the students: ";
      std::cin >> data.name;
      std::cout << "Enter the student's score: ";
      std::cin >> data.score;

      students.push_back(data);
   }
   std::cout << '\n';

   for (size_t loop = 0; loop < students.size(); loop++)
   {
      std::cout << students[loop].name << "\t" << students[loop].score << '\n';
   }
}

How many records will be entered? 3
Enter the names and scores of the students: joe
Enter the student's score: 1.5
Enter the names and scores of the students: mary
Enter the student's score: 2.7
Enter the names and scores of the students: frank
Enter the student's score: 3.9

joe     1.5
mary    2.7
frank   3.9

Oh ok that makes sense, thank you. Although I do not know how to use vectors very well so I have a follow up question. If I wanted to sort the values being entered so that they would be in descending order from highest score to lowest score how would I do that now?
closed account (E0p9LyTq)
If you wanted to sort then you could use a std::map or std::multimap container. No need for the structure if using one of those containers.

You could use a student's name as the mapped element and their score as the key value.

A map/multimap automatically sorts the container, based on the key value.

http://www.cplusplus.com/reference/map/map/

Since it is likely to have the same score for more than one student, a multimap would work

http://www.cplusplus.com/reference/map/multimap/
Last edited on
If you want to stick with a vector just use std::sort to sort it.
http://www.cplusplus.com/reference/algorithm/sort/
closed account (E0p9LyTq)
You are working with two different data types for each student record, you could use std::pair.

Creating a few custom comparison functions for std::sort you can sort by name or score, ascending or descending order:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string>
#include <vector>
#include <utility> // for std::pair
#include <limits>
#include <algorithm> // for std::sort

bool sort_pairfirst_desc(std::pair<std::string, double> i, std::pair<std::string, double> j)
{
   return (i.first > j.first);
}

bool sort_pairsecond_asc(std::pair<std::string, double> i, std::pair<std::string, double> j)
{
   return (i.second < j.second);
}

bool sort_pairsecond_desc(std::pair<std::string, double> i, std::pair<std::string, double> j)
{
   return (i.second > j.second);
}

int main()
{
   std::vector<std::pair<std::string, double>> students;
   std::pair<std::string, double> student;

   while (true)
   {
      std::string name;

      std::cout << "Enter the name of the student (QUIT to quit): ";
      std::getline(std::cin, name);
      if ("QUIT" == name)
      {
         break;
      }

      double score;
      std::cout << "Enter the student's score: ";
      std::cin >> score;
      
      student = std::make_pair(name, score);
      students.push_back(student);

      // flush the input buffer
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   }
   std::cout << '\n';

   std::cout << "The unsorted list:\n";
   for (auto it : students)
   {
      std::cout << it.first << ":\t"<< it.second << '\n';
   }

   std::sort(students.begin(), students.end());

   std::cout << "\nThe default sorted list:\n";
   for (auto it : students)
   {
      std::cout << it.first << ":\t" << it.second << '\n';
   }

   std::sort(students.begin(), students.end(), sort_pairfirst_desc);

   std::cout << "\nThe descending name sorted list:\n";
   for (auto it : students)
   {
      std::cout << it.first << ":\t" << it.second << '\n';
   }

   std::sort(students.begin(), students.end(), sort_pairsecond_asc);

   std::cout << "\nThe ascending score sorted list:\n";
   for (auto it : students)
   {
      std::cout << it.first << ":\t" << it.second << '\n';
   }

   std::sort(students.begin(), students.end(), sort_pairsecond_desc);

   std::cout << "\nThe descending score sorted list:\n";
   for (auto it : students)
   {
      std::cout << it.first << ":\t" << it.second << '\n';
   }
}

Enter the name of the student (QUIT to quit): joe d'ragman
Enter the student's score: 4
Enter the name of the student (QUIT to quit): joe rogan
Enter the student's score: 12
Enter the name of the student (QUIT to quit): mary poppins
Enter the student's score: 3
Enter the name of the student (QUIT to quit): frank furter
Enter the student's score: 8
Enter the name of the student (QUIT to quit): QUIT

The unsorted list:
joe d'ragman:   4
joe rogan:      12
mary poppins:   3
frank furter:   8

The default sorted list:
frank furter:   8
joe d'ragman:   4
joe rogan:      12
mary poppins:   3

The descending name sorted list:
mary poppins:   3
joe rogan:      12
joe d'ragman:   4
frank furter:   8

The ascending score sorted list:
mary poppins:   3
joe d'ragman:   4
frank furter:   8
joe rogan:      12

The descending score sorted list:
joe rogan:      12
frank furter:   8
joe d'ragman:   4
mary poppins:   3

(If this program gets much larger the layout could benefit from putting portions of the code in main() into other functions, making it easier to maintain and read.)
Last edited on
Topic archived. No new replies allowed.