I have been having some problems with strtok.

I'm not very experienced in C/C++ so I'm not used to using strtok.

the code I have is this:

char *cmnd;
    string cmd[100];
    char inpt[81];
        
        while(1)
        {
                cout << "statsh$: ";
                cin.getline(inpt,' ');
                cmnd = strtok(inpt," ");
                int c = 0;
 
                while(cmnd != NULL)
                {
                    cmd[c] = cmnd;
                    cout << cmd[c] << endl;
                    c++;
                    cmnd = strtok(NULL, " ");
                }   
    
                cout << c << endl;
                if(cmd[0] == "exit")
                {
                    break;
                }
                for(int i=0; i < c; i++)
                {
                    cout<< cmd[i] << endl;
                }
        }

The thing is here, that if I type out 8 or more words spaces between each, this program goes haywire and runs over and over the prompt statsh$ until it crashes.

If i enter a a a a a a a a a, as a single char with spaces it will take that just fine however. I have no idea what is going on with this strtok crapola. If someone could help me I would much appreciate it.

Perhaps it's more to do with entering more than 80 characters rather than the number of words.

How does getline for example know how many characters to input, and not overflow the buffer?
According to my info, the 2nd parameter is a length, not a char to stop at.

Perhaps it's more to do with entering more than 80 characters rather than the number of words.

How does getline for example know how many characters to input, and not overflow the buffer?
According to my info, the 2nd parameter is a length, not a char to stop at.

And you were right. I figured it out last night after looking at getline again, because i tried another example that was working and was wondering what the crap was going on. So it was getline. Thanks for the tip anyway.

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.