program "stopped working" error!

I wrote this program to display the average of grades input into the program. We need to have a list with max size of 6, so anything after that 6th entry should be discarded, and not included in the printed output, calculation of the min/max, or average.

As I have it right now, my program will compile and run, but after everything is printed, the program will crash if there were more than 6 entries, and I get the "myList.exe has stopped working" error. Are the two issues related? What can I do to fix these errors?

Here's the 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <fstream> 
#include <iostream> 
using namespace std; 


#include <cstdlib>

// search for A grades
bool hasA(int* score, int MAX_SCORES)
{  
  int i;
  for (i = 0; i < MAX_SCORES; i++) 
  { 
    if (score[i] >= 90) 
    { 
      return true;
      break; 
    } // if
  } // for 
  return false;
}

// search for B grades
bool hasB(int* score, int MAX_SCORES)
{  
  int i;
  for (i = 0; i < MAX_SCORES; i++) 
  { 
    if (score[i] >= 80 && score[i] < 90) 
    { 
      return true;
      break; 
    } // if
  } // for 
  return false;
}

// search for C grades
bool hasC(int* score, int MAX_SCORES)
{  
  int i;
  for (i = 0; i < MAX_SCORES; i++) 
  { 
    if (score[i] >= 70 && score[i] < 80) 
    { 
      return true;
      break; 
    } // if
  } // for 
  return false;
}

// search for passing grades
bool hasPass(int* score, int MAX_SCORES)
{  
  int i;
  for (i = 0; i < MAX_SCORES; i++) 
  { 
    if (score[i] >= 70) 
    { 
      return true;
      break; 
    } // if
  } // for 
  return false;
}


int numberGradesGreater(int* score, int n, double x)
{
  int nGreater = 0;

  for (int i = 0; i < n; i++)
    if (score[i] >= x) nGreater++;
  return nGreater;
}


int compare(const void* pa, const void* pb) 
{ 
  const int& a = *static_cast<const int*>(pa); 
  const int& b = *static_cast<const int*>(pb); 
  if (a < b) return -1; // negative if a<b 
  if (a > b) return 1; // positive if a>b 
  return 0; // 0 for tie 
} // compare

double getAverage(int* score, int n)
{
  int sum = 0;
  double average = 0.0;
  
  for (int i = 0; i < n; i++)
    sum += score[i];
  
  average = double(sum) / n;
  return average;
}
  
int main() 
{ 
  cout << "Editor(s) used: XP Notepad" << endl; 
  cout << "Compiler(s) used: VC++ 2010 Express" << endl;
  cout << "Description: This program prompts the user to enter a variable" << endl;
  cout << " number of scores, displays the input, calculates average, displays" << endl;
  cout << " the minimum and maximum values, and notifies the user if any 'A'," << endl;
  cout << " 'B', 'C', and 'passing' scores were entered." << endl; 
  cout << endl;

  int aScore = 0;
  int SIZE = 0; 
  const int MAX_SCORES = 6;
  int score[MAX_SCORES];

  // prompt user for size

    cout << "How many scores? ";
    cin >> SIZE; 
    cin.ignore(1000, 10);
 

  // prompt user for scores 
  int i; 
  for (i = 0; i < SIZE; i++) 
  { 
    cout << "Enter score " << (i + 1) << ": "; 
    cin >> score[i]; 
    cin.ignore(1000, 10); 

    if (SIZE < MAX_SCORES)
      score[SIZE++] = aScore;

  } // for  

if (SIZE != 0)
{
  // sort scores
  qsort(score, MAX_SCORES, sizeof(int), compare);
  
  // display scores
  for (i = 0; i < MAX_SCORES; i++) 
    cout << score[i] << ' '; 
  cout << endl; 

  // display min, max, and average
  cout << "Minimum: " << score[0] << endl;
  cout << "Maximum: " << score[MAX_SCORES-1] << endl;
  cout << "Average: " << getAverage(score, MAX_SCORES) << endl;

  // display whether or not each letter grade entered
  if(hasA(score, MAX_SCORES))
    cout << "At least one 'A' grade entered." << endl;
  else
    cout << "No 'A' grades entered." << endl;

  if(hasB(score, MAX_SCORES))
    cout << "At least one 'B' grade entered." << endl;
  else
    cout << "No 'B' grades entered." << endl;

  if(hasC(score, MAX_SCORES))
    cout << "At least one 'C' grade entered." << endl;
  else
    cout << "No 'C' grades entered." << endl;

  if(hasPass(score, MAX_SCORES))
    cout << "At least one 'passing' grade entered." << endl;
  else
    cout << "No 'passing' grades entered." << endl;
} 

else if (SIZE == 0)
  cout << "Nothing to display" << endl;


  cout << endl;

  return 0; 
} // main  
Last edited on
If SIZE is larger than MAX_SCORES then you'll write to memory that isn't actually used in your program. That means that pink elephants will fly out of your monitor or you'll accidently create skynet or something undefined. Fortunately it has only resulted in a crash.

To protect against this replace line 124 with:
for (i = 0; i < SIZE && i < MAX_SCORES; i++)

OR replace line 113 with
int score[100];
where 100 is greater than a number that you'd expect the user to input.

If you want to accept any number of inputs, you'll need to learn dynamic memory allocation or try out the vector library.
I really cant do either of those - the requirement is that the list be limited to a max of 6 values, but that the use can enter as many as they want (but only the first 6 are used for calcs and printed) i tried that, but it ended the loop at 6 enteries. we didn't cover dynamic memory allocation (i dont think) and certainly not vectors.
Add a dummy int somewhere and change line 127 to
1
2
3
4
if (i < MAX_SCORES)
    cin >> scores[i]
else
    cin >> dummy;


Also remove the if statement at line 130, and initialise your scores array to all zeros before the loop.
Last edited on
Topic archived. No new replies allowed.