Program crashes when I try to run

I am trying to test my code for an assignment but the console in visual studio crashes before I can see any output. The program is supposed to read in two integers, one for a quiz score (corresponds to the index in the array) and one for the frequency (number of students who received that quiz score). I am supposed to read in quiz scores and frequencies until the sentinel data value is reached (-1 0). When I run the program, I enter in some samples data such as:
35 5
50 2
41 6
...
-1 0

After the -1 0, my program crashes. I don't know why my program crashed. Any help would be appreciated!

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
  #include <iostream>

using namespace std;

//Function prototypes
int lowestQuizScore(int a[]);
int highesstQuizScore(int a[]);
int largestFrequency(int a[]);

const int MAX = 51;			//0 - 50 for 51 possible quiz score values
const int SENTINEL = -1;	//denotes end of data set 

int main()
{
	system("cls");	//clears extraneous texts

	cout << "Welcome to the Quiz Score Frequency Analyzer, by " << endl;
	cout << " " << endl;	//spacing
	cout << "Enter a list of pairs of values: \"QuizScoreValue ScoreCount\". " << endl;
	cout << "Example: 35 5 indicates 5 more students received a score of 35." << endl;
	cout << "Enter \"-1 0 \" when finsihed: " << endl;

	int myArray[MAX];	//initialize array to the number of elements 

	for (int i = 0; i < MAX; i++)
	{
		myArray[i] = 0;	//initialize all elements in the array to 0 (frequency count for scores)
	}
	
	int quizScore;
	int frequency;
	cin >> quizScore >> frequency;	//reads in quiz score and reads in frequency associated with quiz score
	while (quizScore != SENTINEL)	//while quizScore does not equal -1
	{
		myArray[quizScore] += frequency;	//adds frequency to associated quiz score 
		cin >> quizScore >> frequency;
	}

	int lowestScore = lowestQuizScore(myArray);	//returns the lowest quiz score
	int highestScore = highesstQuizScore(myArray);	//returns highest quiz score
	int largestFrequencyCount = largestFrequency(myArray);	//returns largest frequency count

	cout << "The smallest score value is " << lowestScore << endl;
	cout << "The largest score value is " << highestScore << endl;
	cout << "The largest frequency count is " << largestFrequencyCount << endl;







}

//This function.......
int lowestQuizScore(int a[])
{
	int i = 0;
	int frequency = a[i];
	while (frequency == 0)	//stops when it finds first quiz score with a value in it
	{
		i++;	//increment through the array
		int frequency = a[i];
	}
	return i;
}

//This function....
int highesstQuizScore(int a[])
{
	int i = MAX - 1;	//start at end of array
	int frequency = a[i];
	while (frequency == 0)
	{
		i--;	//decrements through array 
		int frequency = a[i];
	}
	return i;
}

int largestFrequency(int a[])
{
	int max = 0;
	for (int i = 0; i < MAX; i++)
	{
		int num = a[i];
		if (num >= max)
		{
			max = num;
		}
	}
	return max;
}
Last edited on
You should run your program line by line to see which line causes the problem. Then, if you still don't know why that line causes the problem, update your question, so everyone can easily answer.
I think I sort of figured out the issue. My program has an infinite loop, therefore causing it to crash. In lines 56 - 76, the program seemed to keep looping forever. I don't see what the problem is though.
Line 63 should be:
1
2
frequency = a[i]; // not: int frequency = a[i];
The way I would handle this is initialize a biggest grade, smallest grade, and biggest frequency variable with the first input. Then, take input from the keyboard until it equals to -1, 0. If the input of the grade is bigger than the biggest grade, change its value, if the input of the grade is smaller that the smallest grade change its value, and if the frequency is bigger that the biggest frequency change its value. This is 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
#include<iostream>
using namespace std;

int main(){
int helper1, helper2;
cin>>helper1>>helper2;
int big_grade = helper1;       //initializes with the first values
int big_freq  = helper2;
int small_grade = helper1;
while(!cin.eof()){                   //this means, while input from keyboard hasn't stopped
cin>>helper1>>helper2;
if (helper1==-1 && helper2==0){ //use break; so there won't be an infinite loop and stop the while loop
    break;                                       
}
else if(big_grade<helper1){   //checks if big_grade is smaller than grade input
    big_grade=helper1;
}
else if(small_grade>helper1){  //checks if small_grade is bigger than grade input 
    small_grade = helper1;
}
if (big_freq<helper2){    //checks if big_frequency is smaller than frequency input 
    big_freq=helper2;
}
}
cout<<"The biggest grade is: "<<big_grade<<endl;      //output the data.
cout<<"The smallest grade is: "<<small_grade<<endl;
cout<<"The biggest frequency is: "<<big_freq<<endl;
return 0;
}


If you have any questions as with how this code works just ask and I'd be glad to explain. We have to understand something in order to use it.
Last edited on
Oh, right! Simple mistake that I did not catch...a second eye really helps. This fixed my program. Thanks so much.
Oh does it have to be with using functions and arrays, or you can solve it with any way you'd like?
It can be any way I'd like, but its preferred to use functions.
Ah, and also you used system("cls") , and if I'm not mistaken, you need to #include<stdlib.h> in order to use it.
Topic archived. No new replies allowed.