Problem with average[updated]

-edit-
Last edited on
To use a var in an array such as string grades[nost]; I belive (at least in windows) the Var has to be a const. so just add a const before int

const int nost = 0;

When I use a var in an array is usualy use a global const. Hope that helps somewhat.

Im also new to c++ (still in college)

This runs, I only tweekd it a little

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


int main()

{
	int nost = 0;
	string names = " ";
	int score[] = {NULL};
	int num = 0;
	int a;
	double total = 0;
	double average;
	num = 0;
	string grades[] = {NULL};



	cout<<"How many students are there? : ";
	cin >> nost;
	cin.ignore(); // cin.ignore(); will make sure it dose not skip Enter name;

	cout <<"Enter name : ";
	getline(cin, names); /* When getting a string, use getline incase user input has spaces*/

	cout<<"Enter a score : ";
	cin >> score[num];





	int minimum = score[num];  // initialize to first score.
	int maximum = score[num];  // initialize to first score.


	while(num < nost && score[num])
	{
		total += score[num];
		++num;
		if(num < nost)
		{
			cout<<"Enter a score: ";
			cin>>score[num];

			if (score[num] < minimum)
			{
				minimum = score[num];
			}
			if (score[num] > maximum)
			{
				maximum = score[num];
			}
		}
	}
	cout<< "The entered test scores are: ";

	for (a = 0; a < num; ++a)
		cout<<score[a]<<" ";
	average = total / num;
	cout<<endl<<"The average test score is "<<average<<endl;

	cout<<"The highest score is "<<maximum<<" and the lowest score is "<<minimum<<endl;

	system("PAUSE");

}
Last edited on
I don't think you can have variable arrays like that. The size of the array must be known at compile-time; that is, before the program actually runs. What you could do is create an array with 20 scores/names, since that's your max, and just fill the array until the user stops or you reach 20.

Since you don't initialize nost to any value, it has some random garbage data.
I don't think you can have variable arrays like that. The size of the array must be known at compile-time; that is, before the program actually runs. What you could do is create an array with 20 scores/names, since that's your max, and just fill the array until the user stops or you reach 20.

Since you don't initialize nost to any value, it has some random garbage data.
Topic archived. No new replies allowed.