Arrays c++

Task 3: AboveAverage
Write a program to analyse student results and output the names and marks of those whose marks are above average.
The program should prompt the user to enter the number of students to process followed by the name and mark for each student. The names should be stored in an array of strings and the marks in an array of integers. The program should calculate and output the average mark, followed by the name and mark for all students whose marks are above that average.

1
2
3
4
5
 int num = 0;
	string students[num];

	cout << "Enter the amount of students at St Josephs Convent: ";
	cin >> num;


C++ won't allow me to allow the user to determine the size of the array unless its a constant. But if its a constant, the user is not determining the size of the array. Is there a way to get around this?

Cheers
    int num;
    cout << "Enter the amount of students at St Josephs Convent: ";
    cin >> num;
    string students[num];
Last edited on
You need to use dynamic arrays.
http://www.cplusplus.com/doc/tutorial/dynamic/
But if its a constant, the user is not determining the size of the array.
The task doesn't actually state that the user should specify the size of the array. What is says is, "the number of students to process". The latter could be any number which is not greater than the array size.

Possibly, define a fixed-size array which is large enough to handle the task, say 30 or 50 or 100 elements. Make sure that the number requested by the user is not larger than this maximum size.

Or use vectors - when you have learned about them.
Last edited on
Thanks, I got my program working!

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int num = 0;
	double average = 0.0;

	cout << "Enter the amount of students at St Josephs Convent: ";
	cin >> num;

	string* students = new string[num];
	int* grade = new int[num];

	getline(cin, students[0]);

	for (int i = 0; i < num; i++)
	{
		cout << "\nEnter name of student " << i + 1 << ": ";
		getline(cin, students[i]);
	}

	for (int j = 0; j < num; j++)
	{
		cout << "\nEnter grade of student " << students[j] << ": ";
		cin >> grade[j];

		average += grade[j];
		
	}

	average /= num;
	cout << endl << "Average: " << average << endl << endl;

	for (int k = 0; k < num; k++)
	{
		if (grade[k] > average)
		{
			cout << endl << students[k] << " was greater than the average" << endl << endl;
		}
	}

	system("pause");
	return 0;
}


Just one question tho, why do I need the getline(cin, students[0]); on line 15 to make the program run normally? If I did not have it there it would skip the first persons name...but this only occurs when I am using the dynamic arrays. If I where not using dynamic arrays I wouldn't need the getline(); why is this?
Last edited on
why do I need the getline(cin, students[0]); on line 15 to make the program run normally?

That's a kind of a workaround. What you really need to do is to remove the newline character '\n' which remains in the input buffer after you did
 
    cin >> num;

After that, you should do as a minimum,
 
    cin.ignore(); 
though it is common to do something like
 
    cin.ignore(1000, '\n');

The first just reads and discards a single character from the input buffer, the latter will ignore up to 1000 characters, or until a newline is read.

This is a common problem, cin >> and getline(cin don't always play nicely together unless you clean up after the cin >>

One more comment. When you use new [], it is good practice to have a matching delete []. This avoids memory leaks.
It may not matter much when the program is about to end, but properly you should have
1
2
    delete [] students;
    delete [] grade;
when you have finished using the dynamically allocated arrays.
Last edited on
Thanks alot chervil for the explanation!
Topic archived. No new replies allowed.