Why won't it print my sorted array?

Basically I had to make an array with values from user input. After I have the array I need to sort it. I am pretty sure I successfully sorted it... but I can't get it to print sorted. I get an error popup that the console has stopped working. I am new to cpp obviously.

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
// Unit 9 modularized.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

// Declare functions.
int askStudents();
int* askMovies(int getStudents);
int* sortMovies(int* askMovies);


//Declare variables.
int getStudents;
int* setMovies;

int main()
{
	getStudents = askStudents();
	askMovies(getStudents);
	sortMovies(setMovies);

    return 0;
}

int askStudents()
{
	cout << "How many students were surveyed? ";
	cin >> getStudents;

	while (getStudents < 0)
	{
		cout << "Please enter a non-negative number: ";
		cin >> getStudents;
	}
	return getStudents;
}

int* askMovies(int getStudents)
{
	// Creating array "setMovies" with size of getStudents using pointer.
	int *setMovies;
	setMovies = new int[getStudents];

	// Storing the amount of movies each student watched into setMovies array.
	for (int i = 0; i < getStudents; i++)
	{
		cout << "How many movies did each student watch: ";
		cin >> setMovies[i];

		while (setMovies[i] < 0)
		{
			cout << "Please enter a non-negative number: ";
			cin >> setMovies[i];
		}
	}

	// Printint setMovies for test
	for (int i = 0; i < getStudents; i++)
		cout << setMovies[i];

	return setMovies;
}

int* sortMovies(int* setMovies)
{
	for (int i = 0; i < getStudents; i++)
	{
		for (int j = i + 1; j <getStudents; j++)
		{
			if (setMovies[i]>setMovies[j])
			{
				int temp = setMovies[i];
				setMovies[i] = setMovies[j];
				setMovies[j] = temp;
			}
		}
	}

	for (int i = 0; i < getStudents; i++)
		cout << setMovies[i];

	return setMovies;
}


You have global variables and local variables with same name. The setMovies masks setMovies in askMovies.
So how do I fix the problem? If I try changing the name of the variable in the sort function it won't work.
Remove lines 16 and 17. Insert necessary local variables.

it won't work

What exactly, and in which way?
Last edited on
Topic archived. No new replies allowed.