Program compiles, but crashes

Hello,

I wrote a program to calculate the average of some scores and sort them in ascending orders. It compiles, but the crashes after I enter the number of scores to be entered. Anybody see what is causing this issue? Thank you.

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>

using namespace std;

void getGrades(double*, int);
void sort(double*, int);
double average(double*, int);
void displayGrades(double*, int, double);
int numScores;
double score;

int main()
{
// Get the number of tests
	cout << "Please enter the number of test scores between 1 and 10: " << endl;
	cin >> numScores;

	while (numScores < 1 || numScores > 10)
	{
		cout << "Sorry, please enter a number betweeen 1 and 10. " << endl;
		cin >> numScores;
	}

	return 0;
}
// Get the test scores
void getGrades(double *score, int size)
{
	for (int count = 0; count < size; count++)
	{
		do
		{
			cout << "What is the score of test " << (count + 1) << ":  " << endl;
			cin >> score[count];

			if (score[count] < 0)
			{
				cout << "Score must be a positive number. Please enter another score " << endl;
			}

		} while (score[count] < 0);

	}

}
// Calculate the average of the scores
double average(double *score, int numScores)
{
	double avg_score;
	double total = 0;

	for (int count = 0; count < numScores; count++)
	{
		total += score[count];
	}

	avg_score = (total / numScores);

	return avg_score;
}
// Sort scores in ascending order
void sort(double *score, int size)
{
	bool swap;
	double temp;

	do
	{
		swap = false;

		for (int count = 0; count < (size - 1); count++)
		{
			if (score[count] > score[count + 1])
			{
				temp = score[count];
				score[count] = score[count + 1];
				score[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
}
// Display sorted grades
void displayGrades(double *score, int size, double avg)
{
	cout << "Below are the sorted scores ";
	cout << "Scores";
	cout << "-----------" << endl;

	for (int count = 0; count < size; count++)
	{
		cout << score[count] << endl;
	}
// Display average
	cout << "Score Average:  " << avg << endl;
}

Last edited on
It doesn't seem to crash. What happens is the return 0; at line 25 is executed, thus ending main(). When main ends, the program terminates - in this case with no errors.

If you want the other functions to be executed, you need to add code inside main() to call them.
I wrote a program to calculate the average of some scores and sort them in ascending orders. It compiles, but the crashes after I enter the number of scores to be entered.

Actually the program cannot cause a crash. There is nothing in the function main() which indicates that there should be something that may potentially make your program crash.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
// Get the number of tests
	cout << "Please enter the number of test scores between 1 and 10: " << endl;
	cin >> numScores;

	while (numScores < 1 || numScores > 10)
	{
		cout << "Sorry, please enter a number betweeen 1 and 10. " << endl;
		cin >> numScores;
	}

	return 0;
}
Last edited on
If you want the other functions to be executed, you need to add code inside main()
to call them.


Could you provide me with an example? I am having no luck. Thank you.
Could you provide me with an example? I am having no luck. Thank you.
Well, you shouldn't need to get lucky. Follow some of the examples in your textbook, or tutorial. See Arrays as parameters
http://www.cplusplus.com/doc/tutorial/arrays/


Before calling any other function, the array needs to be defined. (do that inside main() ). Since the code limits the maximum size to 10 scores, you could define it like this:
 
    double array[10];

The first function call would then look like this (insert this at line 24)
 
    getGrades(array, numScores);

So this is what I have now. After it compiles it asks for the grades to be entered and then crashes. Any suggestions on how to finish and display the average and the sorted grades?

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

using namespace std;

void getGrades(double*, int);
void sort(double*, int);
double average(double*, int);
void displayGrades(double*, int, double);
int numScores;
double score;

int main()
{
	
	// Get the number of tests
	cout << "Please enter the number of test scores: " << endl;
	cin >> numScores;

	while (numScores < 0)
	{
		cout << "Sorry, please enter a positive number: " << endl;
		cin >> numScores;
	}
	double array[100];
	getGrades(array, numScores);
	
	return 0;
}
	// Get the test scores
	void getGrades(double *score, int size)
	{
		for (int count = 0; count < size; count++)
		{
			do
			{
				cout << "What is the score of test " << (count + 1) << ":  " << endl;
				cin >> score[count];

				if (score[count] < 0)
				{
					cout << "Score must be a positive number. Please enter another score " << endl;
				}

			} while (score[count] < 0);

		}

	}
	// Calculate the average of the scores
	double average(double *score, int numScores)
	{
		double avg_score;
		double total = 0;

		for (int count = 0; count < numScores; count++)
		{
			total += score[count];
		}

		avg_score = (total / numScores);
	
		return avg_score;
	}
	// Sort scores in ascending order
	void sort(double *score, int size)
	{
		bool swap;
		double temp;

		do
		{
			swap = false;

			for (int count = 0; count < (size - 1); count++)
			{
				if (score[count] > score[count + 1])
				{
					temp = score[count];
					score[count] = score[count + 1];
					score[count + 1] = temp;
					swap = true;
				}
			}
		} while (swap);
	}
	// Display sorted grades
	void displayGrades(double *score, int size, double avg)
	{
		cout << "Below are the sorted scores ";
		cout << "Scores";
		cout << "-----------" << endl;

		for (int count = 0; count < size; count++)
		{
			cout << score[count] << endl;
		}
		// Display average
		cout << "Score Average:  " << avg << endl;
	
}
int numScores;
Should be :
int numScores = 10;
Your program didn't crash for me. As Chervil pointed out much earlier in this thread, if you want your functions average, sort and displayGrades to run ... you do actually need to call them!

Add these calls at the end of main().
Quote:
int numScores;
Should be :
int numScores = 10;

Why 10? The initial value should be zero. In fact, since it was declared in the global namespace it is default initialised to zero already.

Actually it is good practice to avoid the use of global variables. In this case the variabledouble score; at line 11 should be removed completely, it is not needed, and Line 10, int numScores = 0; should be moved inside main() (just before asking the user to input its value).

I know I am being a pain, but hopefully we are almost there. Now it compiles and runs but stops before we can view the results. Anybody have an idea of how to get it to pause before exiting?

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

using namespace std;

void getGrades(double*, int);
void sort(double*, int);
double average(double*, int);
void displayGrades(double*, int, double);

int main()
{
	int size = 100;

	double test_average;

	cout << "Please enter a number of scores to be graded: " << endl;
	cin >> size;

	double test_scores[100];

	getGrades(test_scores, size);

	test_average = average(test_scores, size);

	sort(test_scores, size);

	displayGrades(test_scores, size, test_average);

	return 0;
}
	// Get the test scores
	void getGrades(double *score, int size)
	{
		for (int count = 0; count < size; count++)
		{
			do
			{
				cout << "What is the score of test " << (count + 1) << ":  " << endl;
				cin >> score[count];

				if (score[count] < 0)
				{
					cout << "Score must be a positive number. Please enter another score " << endl;
				}

			} while (score[count] < 0);

		}

	}
	// Calculate the average of the scores
	double average(double *score, int numScores)
	{
		double avg_score;
		double total = 0;

		for (int count = 0; count < numScores; count++)
		{
			total += score[count];
		}

		avg_score = (total / numScores);
	
		return avg_score;
	}
	// Sort scores in ascending order
	void sort(double *score, int size)
	{
		bool swap;
		double temp;

		do
		{
			swap = false;

			for (int count = 0; count < (size - 1); count++)
			{
				if (score[count] > score[count + 1])
				{
					temp = score[count];
					score[count] = score[count + 1];
					score[count + 1] = temp;
					swap = true;
				}
			}
		} while (swap);
	}
	// Display sorted grades
	void displayGrades(double *score, int size, double avg)
	{
		cout << "Below are the sorted scores ";
		cout << "Scores";
		cout << "-----------" << endl;

		for (int count = 0; count < size; count++)
		{
			cout << score[count] << endl;
		}
		// Display average
		cout << "Score Average:  " << avg << endl;

}

I just had to pause it at the end, thanks for the help!
The intended way to run text-console programs is from the text-console, not from the GUI ("double-clicking").

Not that there's anything wrong with pausing the program at the end, but in practical terms, forcing your command-line program to be interactive seriously hurts its' versatility, since it can no longer be used (easily) without a user actively watching it or as part of a filter or pipeline.
Topic archived. No new replies allowed.