accessing arrays with pointers

I have a student stat calculator that i'm working on. My Average function doesn't want to store the average in float Avg. Also not sure how to get my input to accept different amounts of (names + scores)... I have this...for(int i=0; i<17; i++){.. and I tried to switch 17 to Size but this crashes the program.

Cheers

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
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<math>
using namespace std;

class StudentStat
	{
		private:
			int Size ;
			string Names [ 20 ] ;
			int Scores [ 20 ] ;
			float   Avg,
			        StDev,
			        Median ;
			 int   High_Score,
  			       Low_Score,
			       Range; 
			void Sortem ( ) ;
			
		public:
			void Input ( ) ;
			void Calc_Avg ( ) ;
			void Calc_StDev ( ) ;
			void Calc_Median ( ) ;
			void Calc_Range( ) ;  // also calculates the high and low score
			void Output ( ) ;
	} ;
ifstream infile;
ofstream outfile;
/////////////////////////////////////////////////////////	
int main()
{  
	cout << "test test test..."<< endl;
	infile.open("Prog8odd.dat");
	outfile.open("StudentStat.dat");
	
	if(!infile){
		cout<< "not open! " << endl;
		return -1;
	}
	
	StudentStat StudentObj;

	StudentObj.Input();
	StudentObj.Calc_Avg();
	//StudentObj.Calc_StDev();
	//StudentObj.Calc_Median();
	//StudentObj.Calc_Range();

	StudentObj.Output();
	infile.close();
	outfile.close();
return 0;
}
//////////////////////////////////////////////////////////////////////
void StudentStat::Sortem()
{
	
}
//////////////////////////////////////////////////////////////////////
void StudentStat::Input()          //accepts up to 20 name score pairs and calcs
{

	Size = 0;    		
	for(int i=0; i<17; i++){
		infile>> *(Names + Size);   //Names[Size];
		infile>> *(Scores + Size);   //Scores[Size];
		Size++;    //size member variable to keep count   
	}	
}
//////////////////////////////////////////////////////////////////////
void StudentStat::Output()
{
		for(int i=0; i<Size; i++){
			outfile<< setw(10)<<left<<*(Names + i);
			outfile<< setw(20)<< right <<*(Scores+i)<< endl; 
			cout<< setw(10)<<left<< *(Names + i);	
			cout<<setw(20)<<  right <<*(Scores + i) << endl;
		}
		outfile<< "The Average is: "<<Avg<< endl;

		outfile<< endl;	

	
}
//////////////////////////////////////////////////////////////////////////
void StudentStat::Calc_Avg()
{
	float Avg;
	float total = 0;
	
	for(int i; i < Size; i++)
		total+= *(Scores + i);
	Avg = total / Size;
}
Here is the .dat file

Michelle 71
Marcie 99
David 42
Rebecca 83
Jonathan 79
Matthew 77
Rose 7
Melanie 75
Kimberly 73
Scott 72
Bradley 74
Drextell 10
Heidi 70
Alan 68
Pearl 13
Jeanne 43
Heber 55
wrong
1
2
3
4
5
6
7
8
9
void StudentStat::Calc_Avg()
{
	float Avg;
	float total = 0;
	
	for(int i; i < Size; i++)
		total+= *(Scores + i);
	Avg = total / Size;
}


must be
1
2
3
4
5
6
7
8
void StudentStat::Calc_Avg()
{
	float total = 0;
	
	for(int i; i < Size; i++)
		total+= *(Scores + i);
	Avg = total / Size;
}
I see... maybe I need to read up on what to initialize to zero. I modified my code with float total to zero. However.... Still not getting the proper result. I should be getting 59 something, but i get 6.06727e-39. So I deduce that an incorrect value is being stored in either Size, total, or Avg. Any thoughts on this?

and thanks for your help.
It's not storing the correct value in total... because I can replace total with the sum which is 1011 and i get the ...? well i get 50.. should get 59.hmm.. i just tried the same thing for Size... set it to 17 then i get 59. all i can be certain of is that Avg is holding the correct values. Also this means that 1011 is being divided by 20. so size is holding 20 places... how can i change my for loop to use Size instead of 20 in my input function?
Last edited on
Topic archived. No new replies allowed.