Cout all remaining tokens in string?

I've got a for loop to print each remaining token from the input string, however the program crashes when I do place a "print" function in the code. Help please?

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
#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("OpenCMD v1.0.0");
    HICON hIcon = LoadIcon(GetModuleHandle(0),"MAINICON");
   	SetConsoleIcon(hIcon);

    if (argc<2) {
        cerr << "OpenCMD v1.0.0\n(C) Copyright 2013 HackForums";
        getchar();
    return(0);
    } 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 d = 0; // Number of tokens in string array
        unsigned int e = 0; // Extra interger to use with parsing commands from file (Ex. echo ...)


        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;
                d = 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[d] = token;
                        d++;
                    }
                    if (tokens[0] == "color") { // check first token (0)
                        if (tokens[1] == "-green") { // check second token (1)
                            SetColor(10); // do command for the tokenized string read
                        }
                    } else if (tokens[0] == "pause") {
                        getchar();
                    } else if (tokens[0] == "print") {
                        for (e = sizeof(tokens) / sizeof(tokens[0]); e > 1; e--) {
                            cout << tokens[e];
                        }
                    } else {
                        cout << "OpenCMD has read an unknown command: " << tokens[0];
                        getchar();
                    }
                }
            }
            file.close(); // Close File
        }
    }
    return(0);
}
EDIT: Instead of using a for loop, I'm using a while loop (the code in the OP is old.) The while loop somehow cannot support these commands:

1
2
3
color -green
print hello world. I am master!
pause


Here's the updated 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
#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("OpenCMD v1.0.0");
    HICON hIcon = LoadIcon(GetModuleHandle(0),"MAINICON");
   	SetConsoleIcon(hIcon);

    if (argc<2) {
        cerr << "OpenCMD v1.0.0\n(C) Copyright 2013 HackForums";
        getchar();
    return(0);
    } 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 d = 0; // Number of tokens in string array
        unsigned int e = 0; // Extra interger to use with parsing commands from file (Ex. print ...)


        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;
                d = 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[d] = token;
                        d++;
                    }
                    if (tokens[0] == "color") { // check first token (0)
                        if (tokens[1] == "-green") { // check second token (1)
                            SetColor(10); // do command for the tokenized string read
                        }
                    } else if (tokens[0] == "pause") {
                        getchar();
                    } else if (tokens[0] == "print") {
                        e = sizeof(tokens) / sizeof(tokens[0]);
                        while (e >= 1) {
                            cout << tokens[e];
                            e--;
                        }
                    } else {
                        cout << "OpenCMD has read an unknown command: " << tokens[0];
                        getchar();
                    }
                }
            }
            file.close(); // Close File
        }
    }
    return(0);
}
Is having a variable number of elements in a string array legal?
Topic archived. No new replies allowed.