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!

Bump for hope of an answer.

It looks like depending on what the string contains you want to do certain action. You should look into the command pattern. Anyways here is one way.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Command{
    virtual void execute(const std::string& commandString) = 0;
    ~Command() {}
};

struct StoreCommand : Command{
    string& registerA;
    string& registerB;
    
    StoreCommand(string& regA, string& regB): registerA(regA), registerB(regB){}
    
    void execute(const std::string& commandString){
        //get string to store
        size_t startPos = commandString.find_first_not_of("store");
        size_t endPos = commandString.find("in");
        string data = commandString.substr(startPos, endPos - startPos - 1);
        //get which register to store in
        bool storeInRegA = commandString.find("registerA") != string::npos;
        
        if(storeInRegA) registerA = data;
        else registerB = data;
        
    }
};


//other commands...

//the dispatcher
void dispatch(string& regA, string& regB, const std::string& cmdStr)
{
    if(cmdStr.find("store") != string::npos){
        StoreCommand cmd(regA,regB);
        cmd.execute(cmdStr);
    }else{
        //command not supported
      
    }
}
int main (int argc, const char * argv[])
{
    using namespace std;

    string regA;
    string regB;

    string cmdStr = "store Hello World in registerA";
    
    dispatch(regA, regB, cmdStr);
    
    cout << "regA = " << regA << endl;
    cout << "regB = " << regB << endl;

    return 0;
}

Of course there are different ways of doing it. The basic logic is to find the keywords and from those keywords take the appropriate course of actions.

Hmmm.... I'll have to try that out! :)

As you might be able to tell, I'm trying to make my code as C-friendly as possible, as that's going to be what I'm switching to soon. Is there anyway to do this without vectors?

(I know that C doesn't have "strings" but it makes my coding job a bit easier, so I'm still using those :P )

Never mind, I solved it on my own! Thanks for all of your advice though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.