I've recently been trying to get only a certain number of tokens into a char array. Here, I'll try to clarify; "Jill went to the grocery market to buy tomatoes while fapping". What I've been trying to do is get the thing she's buying, get what she was doing (i.e. while laughing, or while fapping), make sure they put the "while" in there, and store it in a string. While the scenario is representative of what my code is intended to do, I'm obviously not interested in what Jill gets at the market, and what she does while getting it.
Here's my code so far;
string registerD;
string registerE;
char * programTokens = NULL;
char storeStr[256];
const char uRegisterD = "regD";
const char uRegisterE = "regE";
...
else // If it's not a number, it has to be a string
{
programTokens = strtok(NULL, " "); // Getting the next token
strcpy(programTokens, storeStr);
if(storeStr == NULL) // If it can't assign anything, then it obviously can't do this operation!
{
cout << "Strncpy for 'storeStr' has failed!" << endl;
exit(EXIT_FAILURE);
}
else
{
cout << "Strcpy for 'storeStr' has succeeded!" << endl;
}
if(!strcmp(uRegisterD, storeStr))
{
cout << "Here!";
if(registerD != "")
{
cout << "ERROR 11: registerD is already full!" << endl;
programTokens = NULL;
break;
}
else
{
strcpy(storeStr, registerD.c_str());
programTokens = NULL;
break;
}
}
if(!strcmp(uRegisterE, programTokens))
{
if(registerE != "")
{
cout << "ERROR 12: registerE is already full!" << endl;
programTokens = NULL;
break;
}
else
{
programTokens = NULL;
break;
}
}
}
...
(There is code both before and after, but it's not necessary.)
(The goal would be for my to be able to store a certain string into the specified "register". For instance; "store Hi DaniWeb in regD" would store "Hi DaniWeb" into registerD. Whilst; "store Bye DaniWeb in regE" would store "Bye DaniWeb" into registerE. So, I need to be able to get the command (store), what they want to to say (anything), the "in" command, and the register.)
I hope I explained it well enough... If I didn't, feel free to ask for clarification!
Thanks!