Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ separators


ghost's Avatar
0 0

i have a simple windows console simulator, and i have a function which handles all the user inputs/commands. but i need to separate the command itself and its parameter. e.g. mkdir is the command for creating a directory. and when the user input is mkdir test, i need to separate the mkdir, to tell the program to create a new dir, and a test, which is the new dirs name. how can i do this? i want for each one string, e.g. command = mkdir; parameter = test;

thanks


Uber0n's Avatar
Member
0 0

Just separate at every space?


ghost's Avatar
0 0

yes. for example "mkdir test"

parameter = test;```
my function then recognize the command and add the test into the vector.

ghost's Avatar
0 0

i have this:

	char separators[] = " ,\n\t";
	char *command;
	char *param;
	command = strtok(input,separators);
	param = strtok(NULL,separators);
	cout << command << endl;
	cout << param << endl;```
but its giving me an error.. whats wrong??

ynori7's Avatar
Future Emperor of Earth
0 0

use a loop to cycle through the string and if str.at(index) is equal to a space, put the substring from 0 to index into one variable (i.e. command) and the rest into another variable (i.e. parameter).

also, you didnt need to create a second thread for this problem. i just posted in that one too.