Console Not Exiting at End of Main Function?

What the heck? For whatever reason, my console doesn't exit at the end bracket of the main() function. Full code:

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 <string>
#include <windows.h>
#include <fstream>
#include <vector>
#include <sstream>

using namespace std;

void SetColor(unsigned short color) {
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon, color);
}

BOOL WINAPI SetConsoleIcon(HICON hIcon) {
    typedef BOOL (WINAPI *PSetConsoleIcon)(HICON);
	static PSetConsoleIcon pSetConsoleIcon = NULL;
	if(pSetConsoleIcon == NULL)
		pSetConsoleIcon = (PSetConsoleIcon)GetProcAddress(GetModuleHandle(("kernel32")), "SetConsoleIcon");
	if(pSetConsoleIcon == NULL)
		return FALSE;
	return pSetConsoleIcon(hIcon);
}

int main(int argc, char* argv[])
{
    SetConsoleTitle("Cmd2");
    HICON hIcon = LoadIcon(GetModuleHandle(0),"MAINICON");
   	SetConsoleIcon(hIcon);

    if (argc<2) {
        cerr << "Cmd2";
    } else {

        string line;
        string cmd;
        string params;
        string token;
        string tokens[5];

        unsigned int a; // first for loop var.
        unsigned int b = 0; //
        unsigned int c = 0; // Spaces in the string to parse
        unsigned int x = 0;


        ifstream file;
        file.open(argv[1]); // Args start a 0, then go up by 1. argv[0] is the Program path.
        if(!file.is_open()) {
            file.close();
            cout << "Error opening file.";
        } else {
            while(!file.eof()) { // While the file isn't ended
                b = 0;
                c = 0;
                x = 0;
                getline(file, line); // get the command's whole line
                if (!line.empty()) { // If the line isn't empty...
                    for (a = 0; a < line.length(); a++) { // Tranform string to lowercase
                        tolower(line.at(a));
                    }
                    while(isspace(line.at(b))) {
                        b++;

                        for(; b < line.length(); b++) {
                            if(isspace(line.at(b))) {
                                c++;

                                // Skip over duplicate spaces & if a NULL character is found, we're at the end of the string
                                while(isspace(line.at(b++))) {
                                    if(line.at(b) == '\0') {
                                        c--;
                                    }
                                }
                            }
                        }
                    }
                    c++;
                    string tokens[c]; // initialize the number of tokens to number of spaces + 1
                    stringstream ss(line);
                    while (getline(ss, token, ' ')) { // add tokens to array
                        tokens[x] = token;
                        x++;
                    }
                    if (tokens[0] == "color") { // check first token (0)
                        if (tokens[1] == "-lime") { // check second token (1)
                            SetColor(10); // do command for the tokenized string read
                            cout << "Command Completed Successfully.\n"; // tell user command was finished
                        }
                    }
                    if (tokens[0] == "pause") {
                        getchar();
                    }
                }
            }
            while (file.eof()) {
                file.close(); // Close File
            }
        }
    }
    getchar();
    return(0);
}
It seems this is due to that the loop is infinite

1
2
3
            while (file.eof()) {
                file.close(); // Close File
            }
Thanks. Such a stupid mistake...
Topic archived. No new replies allowed.