Function Returns Empty String Every TIme

Excuse me, Im trying to create a command line application in which I have a function that is supposed to give me what # argument was typed in. For example, consider the user input:
connect 127.0.0.1 80

My function, command(string textEntered, int arg,),
when used as command("connect 127.0.0.1 80",0), is supposed to return "connect"

Now, since I'm stupid, I can't get it to work. Every time it decides to return an empty string.
Here is the function:

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
string command(string textEntered, int arg){
    //This is where we keep all the commands the user has entered, 1 by 1.
    string arrays[10];
    //Used to record the string we will write to arrays[] in the for loop
    string loopstring;
    //Counter
    int arraycounter = 0;
    //Goes through all the characters in textEntered
    for(int i=0;i<=textEntered.length();i++){
        //If we encounter a space
        if(textEntered[i]==' '){
            //Log the word before it and update the arrays index we are writing to
            arrays[arraycounter] = loopstring;
            //Increment it, since we want to start logging a new word
            arraycounter++;
            //Clear the string we use to define the arrays variable
            loopstring="";
        } else {
           loopstring[i]=textEntered[i];
        }
    }
    //If there was only one command we'll just return textEntered
    if(arraycounter=0){
        return textEntered;
    } else {     //Otherwise we return the index they want
        return arrays[arg];
    }
}
Last edited on
Split arguments by spaces. There are numerous ways to split a string. (See http://www.cplusplus.com/faq/sequences/strings/split/.)

This caches the command -- you only need to split it up once. After that, getting a specific argument is a simple lookup.

1
2
3
4
5
6
7
8
9
10
11
12
  while (true)
  {
    string s;
    if (interactive) cout << "> ";
    if (!getline( inf, s )) break;

    vector <string> cmdargs;
    split( cmdargs, s, " " );
    if (cmdargs.empty()) continue;

    cout << "the command has " << (cmdargs.size() - 1) << " arguments.\n";
  }

Hope this helps.
I think that Duoas has a much better answer, but I hope that this can help too.

I wrote the same function as you, but instead of using arrays I used vectors. I think that vectors are a bit better suited here, because the user can input any amount of arguments that they want to.

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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>

std::string getCommand(std::string, int);

/* the main entry point */
int main(void)
{
	std::cout << getCommand("connect 127.0.0.1 0", 0); /* connect */
	std::cout << getCommand("connect 127.0.0.1 0", 1); /* 127.0.0.1 */
	std::cout << getCommand("connect 127.0.0.1 0", 2); /* 0 */
}

/* get the command from a given string */
std::string getCommand(std::string line, int arg)
{
	/* where we keep the split string */
	std::vector<std::string> split;
	
	/* temporary string */
	std::string temp = line;
	
	/* split the string into parts */
	while (temp.size() != 0)
	{
		if (temp.find(char(0x20)) != std::string::npos)
		{
			/* get the part of the string */
			split.push_back(temp.substr(0, temp.find(0x20)));
			
			/* trim the temporary string */
			temp = temp.substr(temp.find(char(0x20))+1);
			
			continue;
		}
		
		/* get the part of the string */
		split.push_back(temp);
		
		break;
	}
	
	/* does the given argument exist */
	if ((arg >= 0) && (arg < split.size()))
	{
		/* return that argument */
		return split[arg];
	}
}


I hope that my comments were clear enough to explain what the function was doing. Duoas is right in that the better option would be to split up the string, then save the parts. Then when you are looking for a piece of it, you just find the piece that you stored.

Edit: A better name for the function would be like getPiece or something. getCommand infers that we are only looking at the command. My mistake.
Last edited on
Topic archived. No new replies allowed.