Having trouble assigning value to an array

Pages: 123
Yeah, well, programmers stopped using globals to pass parameters and return values decades ago. And for a very good reason you'll find out soon enough if you keep doing it.
Look at what I'm doing in the example and learn how to do it yourself.
(The actual operation is irrelevant. I just put in the for loop to demonstrate array indexing.)
Alright let me take a look at the example and try to fine tune my program properly.
I'll try to limit my use of globals and see what I get.
Hang on, could you explain where the linker errors coming from atleast?
Because I understand the idea of indexing, and I know you don't have to return 0 (I just normally do), but above all I am just not seeing where this linker error's coming from, or exactly how to fix it.

Because what's irritating me about it is that I can remove every trace of them from my program yet the error still comes up.
Last edited on
You probably made an error in a function definition. What does the error say?
For this code:
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
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int ExamScores[];
int num;
int getLabScores();
int LabScores[];
int i;
int calculatePointGrades();
int LSavg[];










int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today?\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	
	
	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
	}
	else 
	{ 
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> ExamScores[num];
		}
		

	int * Examscores = 0;
	Examscores = new int;
	Examscores = &ExamScores[0];
	}
	return 0;
}

int getLabScores()
{



for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> LabScores[num];
	
}
for (num =0; num < noofstu; num++)
{
	 LSavg[num] = LabScores[num] * .30;
	cout << LSavg[num];

}
return 0;
}





int main ()
{ //Open main
	

	getStudentCount();
	getExamScores();
	getLabScores();



	cin.ignore(2);
return 0;
} // Close main 


I get these errors:

>NewLab.obj : error LNK2001: unresolved external symbol "int * ExamScores" (?ExamScores@@3PAHA)
1>NewLab.obj : error LNK2001: unresolved external symbol "int * LSavg" (?LSavg@@3PAHA)
1>NewLab.obj : error LNK2001: unresolved external symbol "int * LabScores" (?LabScores@@3PAHA)

And not that it's entirely hard to do given my experience, but i'm baffled.

btw- I added the LSavg because it's the next step I've got to do.
Last edited on
Lines 10, 13, and 16: These arrays don't have size.
I added a limit to the amount of inputs. Made the size 100, will test.
Last edited on
Once again I know I'm over looking something small, and I know I've solved problems similar to this, but I need another set of eyes.

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <math.h>
#include <iomanip> 

using namespace std;
int const MAXSTUDENTS = 100;
int getStudentCount();
int noofstu;
int getExamScores();
int ExamScores[100];
int num;
int getLabScores();
int LabScores[100];
int i;
int calculatePointGrades();
int LSavg[100];
int ESavg[100];










int getStudentCount()
{
	cout << "Calculating Students Grades" << endl;
	cout << "\nHow many students grades would you like to enter today? (Up to 100)\n" << endl;
		cin >> noofstu;
		return 0;
}

int getExamScores()
{
	
	
	
	
		for (num=0; num < noofstu; num++)
		{
			int n = num + 1;
	cout << "\nEnter Exam Score(s) for student "<< n++ << endl;
			cin >> ExamScores[num];
		}

		for (num =0; num < noofstu; num++)
{
	 ESavg[num] = (ExamScores[num]* 0.30) ;

	cout << "Here's the avg " << setprecision(2) << LSavg[num] << endl;

}

		

	int * Examscores = 0;
	Examscores = new int;
	Examscores = &ExamScores[0];
	
	return 0;
}

int getLabScores()
{



for (num =0; num < noofstu; num++)
{
	int n = num + 1;
	cout << "\nEnter Lab Score(s) for student "<< n++ << endl;
	cin >> LabScores[num];
	
}
for (num =0; num < noofstu; num++)
{
	 LSavg[num] = LabScores[num] * 0.30;

	cout << "Here's the avg " << setprecision(2)<< LSavg[num] << endl;

}
return LSavg[0];
}





int main ()
{ //Open main
	

	getStudentCount();

	if (noofstu == 0)
	{
		cout << "You have entered no students, try again" << endl;
		return 0;
	}

	else if (100 < noofstu)
	{
		cout << "You have exceeded the limit, try again" <<endl;
		return 0;
	}

	else 
	{
	getExamScores();
	getLabScores();
	}



	cin.ignore(2);
return 0;
} // Close main 


I'm trying to get the avgs of those two LSavg, ESavg.
I'm getting these warnings:
(51) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
(80) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Alright I know why it's happening, i think (decimal being declared double and being forced to convert to int?), but I can't remember how to fix it.

Now the loss of data only occurs for the first avg not the second...I get all the avgs correctly for the second but the first I get 0....

ESavg[num] = (ExamScores[num]* 0.30) ;

ESavg and ExamScores are arrays of integers. When you multiply an integer by 0.30, the result isn't an integer anymore and the decimal part gets truncated. The loss of data here is the loss of precision, and the compiler is warning that you might not want that.

(I still remember in the old days when I used Turbo C++ version 3.0 or something, the compiler wasn't smart enough to warn you in this situation, and I would get results that didn't make sense.)

This might be an obvious suggestion, but why not use floats or doubles for some of the variables instead?
Last edited on
You can ignore those warnings, or you can make all your ints into floats/doubles. It's probably safer to go with the latter.
Last edited on
The first thing I went to do was make them double and then float, but alas it gave me a warning the same warning. I'll retry it see what I get.
Deleted.
Last edited on
Got the program built and working.
Compiler's still being wonky but that's another story.

Thank you very much everyone who helped me.
Like I said earlier if it wasn't for the help I'd be rocking back and forth in the fetal position in the corner.

Congrats :D
Topic archived. No new replies allowed.
Pages: 123