Cannot Get this program to stay open in the cmd

closed account (9L8T0pDG)
When I run the code, the ouput does not stay open in the cmd even if I use system("PAUSE"); or ignore.

What is going on?

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

using namespace std;

const int STUDENTS = 20;
int x;
struct studentType
{
    string FN;
    string LN;
    int Score;
    char grade;
};

void getData(ifstream& inFile, studentType sList[], int listSize);
void calculateGrade(studentType sList[], int listSize);
int highestScore(const studentType sList[], int listSize);
void printResult(ofstream& outFile, const studentType sList[], 
                 int listSize);

int main()
{
    ifstream inData;
    ofstream outData;
    studentType studentList[STUDENTS];

    inData.open("Ch11_Ex2Data.txt");
    if (!inData)
    {
        cout << "Input file cannot be found. Terminating. . ."
             << endl;
        return 1;
    }

    
    getData(inData, studentList, STUDENTS);
    calculateGrade(studentList, STUDENTS);
    printResult(outData, studentList, STUDENTS);

    
	exit(0);
}

void getData(ifstream& inFile, studentType sList[], int listSize)
{
    for (int i = 0; i < listSize; i++)
        inFile >> sList[i].FN >> sList[i].LN
               >> sList[i].Score;
}

void calculateGrade(studentType sList[], int listSize)
{
    for (int i = 0; i < listSize; i++)
    {
        switch (sList[i].Score / 10)
        {
        case 10: 
        case 9: 
            sList[i].grade = 'A';
            break;
        case 8: 
            sList[i].grade = 'B';
            break;
        case 7: 
            sList[i].grade = 'C';
            break;
        case 6: 
            sList[i].grade = 'D';
            break;
        case 5: 
        case 4: 
        case 3: 
        case 2: 
        case 1: 
        case 0: 
            sList[i].grade = 'F';
        }
    }
}

int highestScore(const studentType sList[], int listSize)
{
    int hScore = sList[0].Score;

    for (int i = 1; i < listSize; i++)
        if (hScore < sList[i].Score)
            hScore = sList[i].Score;

    return hScore;
}

void printResult(ofstream& outFile, const studentType sList[], 
				 int listSize)
{
    int maxScore = highestScore(sList, listSize);
    int i;

    outFile << setw(15) << "Student Name           "
            << setw(10) << "Test Score"
            << setw(7) << "Grade" << endl;

    for (i = 0; i < listSize; i++)
        outFile << left << setw(25)
                << sList[i].LN + ", " + sList[i].FN
                << right << " " << setw(5) << sList[i].Score
                << setw(6) << " " << sList[i].grade << endl;

    outFile << endl << "Highest Test Score: " << maxScore << endl;
    outFile << "Students having the highest test score:" << endl;

    for (i = 0; i < listSize; i++)
        if (sList[i].Score == maxScore)
            outFile << sList[i].LN + ",  " + sList[i].FN
                    << endl;
}

Hi , Which envirment (editor ) you are using .. aslo have you tried with simple getch() ;
I don't think it's a good idea to use commands like system("PAUSE");. It is not part of the c++ and it's potentially dangerous.

Also use return 0; instead of exit(0); as the former calls the destructor of any locally object.

Anyway do you need to see the output of your program? If you use a terminal (console, dos prompt or what ever is the equivalent to your system) you can check it out even when the program terminates.

Another possibility is just to expect for a user input as bluecoder mentioned. Although I don't think that getch() is the best choice since it's unbuffered input, though in that case harmless. You can use:
std::cin.get(); instead.
closed account (9L8T0pDG)
Yeah I have tried std::cin.get(); and getch();

Nothing Stops the cmd from closing.

I am using VS 2008 btw
closed account (9L8T0pDG)
Any one?
Topic archived. No new replies allowed.