Program Wont stay open to see the output

Write your question here.



#include <iostream>
#include <cstdlib>
using namespace std;

double median(int , int);
double mode(int *, int);
int *makeArray(int);
void getMovieData(int *, int);
void selectionSort(int [], int);
double average(int *, int);

int main ()
{

double average;
int *students;
int numstudents;
double total = 0;
double median;

cout << "How many Students were Surveyed?" << endl;
cin >> numstudents ;

while(numstudents <= 0)
{
cout << "invalid Entry \n";
cout << "Enter number of students? \n";
cin >> numstudents;
}

students = new int [numstudents];
cout << "Enter the number of movies each Student watched \n";

for (int count = 0; count < numstudents; count++)
{
cout << " student#" << (count +1 ) << ": " ;
cin >> students[count];
}

for (int count = 0; count < numstudents; count++ )
{
total += students[count];
average = total / numstudents;
}


cout << "Students Watched on Average: " << average <<" Movies"<< endl;
cout << "the median is" << median << endl;




}

double median (int numbers [], int size)
{

double median;

if ( size % 2 == 0)

double median = (int) (numbers[size /2] + numbers[size / 2 ])/2;

else


median = numbers[size/2];

system("PAUSE");
return 0;



}
When your code exits the main call to go to one of the functions you should put

1
2
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


At the end of it, DON'T use system, the above code is pure C++ thus portable amongst operating systems.
Have you ever run ipconfig or ping from the command prompt? These are also console programs and they will also not keep the command prompt open once they complete.

If you don't want to modify your source code (which you don't have to):
1. open a command prompt (I use WIN+R, then type "cmd").
2. Navigate to your working directory with "cd", or right click and paste the folder address.
3. Type your executable's name and hit "Enter"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int count = 0; count < numstudents; count++ )
{
total += students[count];
average = total / numstudents;
}


cout << "Students Watched on Average: " << average <<" Movies"<< endl;
cout << "the median is" << median << endl; 


 std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

}


That is where I see you need it.
Topic archived. No new replies allowed.