Console closing down in Vs 2008

closed account (9L8T0pDG)
Hello the following code does not stay open even with normal conventions of halting the cmd. Any suggestions?

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
121
122
123
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <limits>

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);

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

  return 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;
}

@DazednConfused

Here is the wait routine I use in my programs.
1
2
3
4
5
6
7
8
9
#include <conio.h> // add to includes if not already there
void WaitKey()
{
cout << "\t\tPress ENTER to continue...\n\t\t"; \\ remove if you already print out a line. 
                                               \\  No sense leaving user guessing 
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}
Last edited on
closed account (9L8T0pDG)
@whitenite1

Thanks but it still wont stay open. . .

This is what the output debugger says:


'nlnjuh.exe': Loaded 'C:\Users\a\Documents\Visual Studio 2008\Projects\nlnjuh\Debug\nlnjuh.exe', Symbols loaded.
'nlnjuh.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll'
'nlnjuh.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll'
'nlnjuh.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcp90d.dll'
'nlnjuh.exe': Loaded 'C:\Windows\winsxs\x86_microsoft.vc90.debugcrt_1fc8b3b9a1e18e3b_9.0.30729.1_none_bb1f6aa1308c35eb\msvcr90d.dll'
The program '[5188] nlnjuh.exe: Native' has exited with code 1 (0x1).
Last edited on
I don't think conio.h is standard or included in VS2008.

Try:
1
2
3
4
5
6
7
8
9
10
11
// Required headers
#include <iostream>
#include <limits>

void Continue()
{
   std::cout << "Press ENTER/RETURN to continue...";
   if (std::cin.rdbuf()->in_avail() != 0)
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
   std::cin.get();
}
Last edited on
closed account (9L8T0pDG)
@wolfgang

That didnt work either.

It does not stay open
@DazednConfused

Please let me know how you are using the WaitKey() routine, because it works perfectly for me. I use MS Visual C++ 2008 Express
closed account (9L8T0pDG)
@whitenite1

I put #include <conio.h> at the top with :

1
2
3
4
5
6
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <limits>
#include <conio.h> 


I then put your method at the end of the program.
@DazednConfused

Since it's at the end, did you prototype it at the top?. void WaitKey(); Are you calling the function?
WaitKey(); If you are doing all this, and it's still not working, please paste your code here, and let me see what's going on. Thanks..
Some of those methods are ridiculously complex. I just add:
1
2
printf("Press any key to continue...\n");
getchar();


Actually, I generally just put a breakpoint on my main's return statement. To hell with additional lines of code.
1
2
3
4
5
6
7
inData.open("Ch11_Ex2Data.txt");
    if (!inData)
    {
        cout << "Input file cannot be found. Terminating. . ."
             << endl;
        return 1;
    }


Does "Ch11_Ex2Data.txt" exist? You said your program was returning 1.

And here's a (Windows-only) pause function that mimics system("pause");

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void pause(const char* str = "Press any key to continue . . . ");

void pause(const char* str)
{
	HANDLE in = GetStdHandle(STD_INPUT_HANDLE),
		out = GetStdHandle(STD_OUTPUT_HANDLE);
	DWORD mode = 0,
		charsWritten = 0;
	char input;
		
	WriteConsoleA(out, str, strlen(str), &charsWritten, NULL);
	
	if(!GetConsoleMode(in, &mode))
		return;
	if(!SetConsoleMode(in, mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)))
		return;
	FlushConsoleInputBuffer(in);
	ReadConsoleA(in, &input, 1, &charsWritten, NULL);
	FlushConsoleInputBuffer(in);
	if(!SetConsoleMode(in, mode))
		return;

	WriteConsoleA(out, "\r\n", 2, &charsWritten, NULL);
}
Last edited on
closed account (9L8T0pDG)
@Branflakes91093

Ch11_Ex2Data.txt does exist. How do I know that this program is using the file?

This still does not stay open! Someone help me.

What is wrong? This hasn't happened since I have started C++.
You said that your program returns 1, and that's the only place in your code that returns 1.

Is Ch11_Ex2Data.txt in the correct spot? Is it spelled correctly?

Why not just use the debugger and step through the code line by line to see what's happening?
closed account (9L8T0pDG)
where is Ch11_Ex2Data.txt supposed to be to work?

I have both Ch11_Ex2Data.txt and the .cpp in the same file on the desktop. Should t be in a different file?
Last edited on
Ch11_Ex2Data.txt should be where nlnjuh.exe is being run from.
closed account (9L8T0pDG)
THANK YOU DEAR GOD or Branflakes91093.

Thanks it works now.
Topic archived. No new replies allowed.