EXE Has Stopped Working

My executable does what it's supposed to, but after everything is completed, it "stops working" and a window pops up on the desktop saying that.

Here's the 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#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 CountWords(string strString)
{
  int nSpaces = 0;
  unsigned int i = 0;

  // Skip over spaces at the beginning of the word
  while(isspace(strString.at(i)))
    i++;

  for(; i < strString.length(); i++)
  {
    if(isspace(strString.at(i)))
    {
      nSpaces++;

      // Skip over duplicate spaces & if a NULL character is found, we're at the end of the string
      while(isspace(strString.at(i++)))
        if(strString.at(i) == '\0')
          nSpaces--;
    }
  }

  // The number of words = the number of spaces + 1
  return nSpaces + 1;
}

void GetTokens(string line) {
    int x = 0;
    string token;
    int y = CountWords(line);
    string tokens[y];
    stringstream ss(line);
    while (getline(ss, token, ' ')) {
        tokens[x] = token;
        x++;
    }
    if (tokens[0] == "color") {
        if (tokens[0] == "-lime") {
            SetColor(10);
        }
    }
}

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
                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--;
                                    }
                                }
                            }
                        }
                    }
                    string tokens[c+1]; // 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
                        }
                    }
                }
            }
            file.close(); // Close File
        }
    }
    getchar();
}
Run it trough a debugger to figure out the line where it crashes.

string tokens[c+1]; is illegal, dimension must be a constant
Oh, silly me, DUH! *facepalm*

Now the program doesn't exit after enter is pressed with getchar();:

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
#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
            }
        }
    }
}
Topic archived. No new replies allowed.