Need help with Array please!

I'm working on an assignment and pretty much can't seem to solve this:

Write a C++ program to search a 2D array and print the first and last names of all students that have scored above a value entered by the user.

the array consist of the following columns:

ID first-name last-name total-score

the array currently holds the information of 5 students but can hold up to 100 students.

For example, if your array consist of the following data:

111 Tom Jones 80

222 Jack Williams 70

333 Sarah Jones 100

444 Judy Thomson 99

555 Sandra Will 65

and we want the names of all students who scored 70 and above, your program will list:


Tom Jones

Jack Williams

Sarah Jones

Judy Thomson

This is what I have so far, and I'm pretty sure it's not a good start. She wants me to print the names of students with the value entered, but how can I do this when my array can only consist of string or if I change the int, then I won't be able to put the names in the array. I'm just confused. Your help would be appreciated, thanks.


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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string a[100][4] = { {"111", "Tom" , "Jones" , "80"}, {"222", "Jack", "Williams", "70"},
	{"333", "Sarah", "Johns", "100"}, {"444", "Judy", "Thomson", "99"}, {"555", "Sandra", "Will", "65"} };
	
	 string target;
	cout << "Please enter a test score: " << endl;
	cin >> target;



	for (int i = 0; i < 5; i++)
	{
		
		if (target == a[i][3])
		{
			cout << "Students who had a " << target << " and above: " << endl;
				cout << a[i][1] << " " << a[i][2] << endl;
		}
	}

	system("Pause");
	return 0;
}
Last edited on
Have you studied classes or structures yet? A simple structure would make this much less complicated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...

struct Student
{
    int id;
    std::string name;
    int grade;
};

...

   std::vector<Student> students{{111, "Tom Jones" , 80}, {222, "Jack Williams", 70}};

...

       if(target == students[i].grade)
             print the student information.

...


Topic archived. No new replies allowed.