Problem with While Loop termination

I am currently trying to fill a double array with a while loop with a terminating condition (entered>0). The array is getting filled and the loop appears to be working however when a -1 is entered the program does not seem to exit the loop. I've been trying to debug this with cout statements for hours and I really appreciate any help.
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
 



#include <math.h>
#include <iostream>
using namespace std;

double calc(double a[],double dev[], int n,double *mean);
void letter(double a[],char letg[],double std,double *mean);

int main(void)
{double a[6],dev[6],mean, std;
int i,n;
double entered;
char letg[6];

 cout<<"Please enter the test grades one at a time (max 6)\n";
 cout<<"enter a -1 when you are done entering scores\n";
 //based off class notes
 i=0;
 cin>>entered; 
while (entered>=0 && i<6)
{a[i]=entered;
  i++; 
  cin>>entered;
  }
i=n;

cout<<"out of loop"<<endl;

std=calc(a,dev,n,&mean);
letter(a,letg,std,&mean);
cout<<"the corresponding scores and letter grades are:\n";
cout<<a;
cout<<letg;
return 0;
}

/*this function calculates mean and standard deviation for each score
Patrick Jones
c++
g++*/
double calc(double a[],double dev[],int n,double *mean)
{int c,i;
cout<<"in calc";
double sum,sqdif,std;
 c=0;
 sum=0;
 while (c<=n)
  {sum=sum+a[c];
   c++;
}
 *mean=sum/(n+1);
 for (i=0;i<=n;i++)
 dev[n]=pow((a[n]-*mean),2);
 

for(i=0;i<=n;i++)
sqdif=dev[i]+sqdif;
std=sqrt(sqdif/c);
 return std;
}


/*This function determines the letter grade given for each score submitted
 Patrick Jones
 c++
 g++*/
 
void letter(double a[],char letg[],double std,double *mean) 
{int temp, i;
 while (a[i]>0)
 temp=((a[i]-*mean)/std);
 if (temp<-1.5)
  letg[i]='f';
 else
  if(temp<-.5)
  letg[i]='d';
 else
 if (-.5<temp<.5)
  letg[i]='c';
 else
 if (.5<temp<1.5)
  letg[i]='b';
 else
  letg[i]='a';
  return;
}

edit:entire code posted, objective is to take a set of scores calculated the standard deviation and assign letter grades.
-i can now exit the loop but the segmentation fault is occurring in my first call to function calc
Last edited on
I don't see anything wrong, other than undefined behavior when i == 6.

http://ideone.com/bDxZvE

It is probably a subtle mistake that is difficult to spot. Can you post your entire code?
Last edited on
Topic archived. No new replies allowed.