Why doesn't this work?

This is supposed to be a simple console but there is some errors with it. Can anyone help me 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
// test command prompt
#include<cstdio>
#include<cstring>
int main() {
    char input[256];
    char *pch;
    bool done = false;
    
    printf("||.:: Command Prompt ::.||\n");
    
    while (!done) {
        printf("O-Shell ->  ");
        pch = gets(input);
        
        pch = strtok(input, " ");
        
        while(pch != NULL) {
            pch = strtok(NULL, " ");
        }
        
        if (pch == NULL) {
            printf("You need to enter a command.\n");
        } else if (strcmp(pch[0], "exit") == 0) {
            if (strcmp(pch[1], "true") == 0) {
                printf("Exiting...\n");
                done = true;
            } else if (strcmp(pch[1], "false") == 0) {
                printf("Exiting... just kidding!\n");
                done = false;
            }
        } else {
            printf("Invalid command.\n");
        }
    }    
    
    return 0;
}


Thanks in advance!
Phil
What errors?

1
2
3
4
while(pch != NULL){
  //...
}// pch must be NULL to end the loop
if(pch == NULL) //true 
It gives me an error in compile... says : "invalid conversion from "char" to "Const char"".. What does that mean?

By the way I took the second while loop out.
Last edited on
1
2
3
4
5
char *pch;//pch is a pointer to char (a cstring)
pch[0]; //this is one character

strcmp(pch[1],  //error expected const char * received char 
           "true");
so how do i fix that error... could you give me an example or something please?
If I understand correctly, you are trying to analize every word of your input.
I will just read them word by word instead of the hole line.
1
2
std::string word;
cin >> word;


You could use the fact that strtok put an '\0' at the end of the substring (not tested)
1
2
3
4
5
while(pch){
  pch = strtok(NULL, " "); //next word
  strcmp( pch, "hello" ); //make all the comparisons
//...
}
Okay it compiles now but it doesn't say invalid command, it actually causes an error in itself now. Hmmm... here is the new code updated for C++ instead of C.

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
// test command prompt
#include<iostream>
#include<cstring>
int main() {
    using namespace std;
    
    string input;
    char *pch;
    bool done = false;
    
    cout << "||.:: Command Prompt ::.||\n";
    
    while (!done) {
        cout << "O-Shell ->  ";
        cin >> input;
        
        strcpy(pch, input.c_str());
        pch = strtok(pch, " ");
        
        while(pch) {
            pch = strtok(NULL, " ");
            
            if (pch == NULL) {
                cout << "You need to enter a command.\n";
            } else if (strcmp(pch, "exit") == 0) {
                if (strcmp(pch, "true") == 0) {
                    cout << "Exiting...\n";
                    done = true;
                } else if (strcmp(pch, "false") == 0) {
                    cout << "Exiting... just kidding!\n";
                    done = false;
                }
            } else {
                cout << "Invalid command.\n";
            }
        }
    }    
    
    return 0;
}


Please any help would be appreciated.
Sorry to keep posting but I fixed the error when it caused an error in itself. Now it just won't execute the command when you type exit true or exit false... hmm.

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
// test command prompt
#include<iostream>
#include<cstring>
int main() {
    using namespace std;
    
    string input;
    char command[128];
    char *pch;
    bool done = false;
    
    cout << "||.:: Command Prompt ::.||\n";
    
    while (!done) {
        cout << "O-Shell ->  ";
        cin >> input;
        
        strcpy(command, input.c_str());
        pch = strtok(command, " ");

        while(pch) {
            if (strcmp(pch, "exit") == 0) {
                if (strcmp(pch, "true") == 0) {
                    cout << "Exiting...\n";
                    pch = false;
                    done = true;
                } else if (strcmp(pch, "false") == 0) {
                    cout << "Exiting... just kidding!\n";
                    pch = false;
                    done = false;
                }
            } else {
                cout << "Invalid command.\n";
                pch = false;
            }
        }
    }    
    
    return 0;
}


Any help would be grately appreciated by me :)
Thanks.
Hi,
This is the working program in C .

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
// test command prompt
#include<cstdio>
#include<cstring>
int main() {
    char input[256];
    char *pch[10]; // array of character pointers
    bool done = false;
    
    printf("||.:: Command Prompt ::.||\n");
    
    while (!done) {
        printf("O-Shell ->  ");
        gets(input);
        int i = 0; 
        if ((pch[i] = strtok(input, " ")) == NULL) {
            printf("You need to enter a command.\n");
	    continue;
	}
	i++;
        while(pch[i] = strtok(NULL, " "))    //assign strtok(NULL," ") to pch[i] and then checks it for NULL (its a shorthand notation)
		i++;

        if (pch[1] && strcmp(pch[0], "exit") == 0) {  
            if (strcmp(pch[1], "true") == 0) {
                printf("Exiting...\n");
                done = true;
            } else if (strcmp(pch[1], "false") == 0) {
                printf("Exiting... just kidding!\n");
                done = false;
            }
        } else {
            printf("Invalid command.\n");
        }
    }    
    
    return 0;
}


Let us look at the changes I've made.
1. In your code , at line 23 and 23 you did :
1
2
 if (strcmp(pch, "exit") == 0) {
 if (strcmp(pch, "true") == 0) {

pch is a pointer to a single string. Its too much obvious that if pch is equal to "exit" then how can it be equal to "true" also !!!!
2. You basically need an array of character pointers where each array element will hold each word of the command. This is what I've done above.
3. In my code , if you write line 23 as
if (strcmp(pch[0],"exit") == 0) instead of what i've written then the program will crash if you give the command as : exit
because then there will be no pch[1] and hence doing strcmp on it will produce a segmentation fault.

This is the program output (Hope this is what up want to achieve )

||.:: Command Prompt ::.||
O-Shell ->
You need to enter a command.
O-Shell ->
You need to enter a command.
O-Shell ->
You need to enter a command.
O-Shell ->  exit false
Exiting... just kidding!
O-Shell ->  exit
Invalid command.
O-Shell ->  exit true
Exiting...

Thank you very much, it worked!
Topic archived. No new replies allowed.