Home > Software design >  How does "to_string" work in array of string?
How does "to_string" work in array of string?

Time:05-25

I want to combine the first 4 characters of each line in txt file and compare it with the keyword I have, but when I combine the characters, I get the sum of these 4 characters' ascii numbers(whatever). How can I solve this problem. My code is here: When I debuged, I saw the string search(variable) was 321.

int main() {
    ifstream file("sentence.txt");
    
    if (file.is_open()) {
        string line;
        while (getline(file, line)) {
            string search = to_string(line[0]   line[1]   line[2]); // you see what I mean
            if ("dog" == search) {
                cout << "there is dog";
            }
            else {
                cout << "there is no dog"<<endl;
            }
        }
    }
}

CodePudding user response:

The function std::to_string() is designed to convert a number into a string representation. It is not what you need.

There is no need to create the new string search to check whether the string line starts with the string "dog".

Creating the new string search is inefficient.

Instead, you could write for example

if ( line.compare( 0, 3, "dog" ) == 0 ) {
    cout << "there is dog";
}
else {
    cout << "there is no dog" << endl;
}

Or, if your compiler supports C 20, you can also write:

if ( line.starts_with( "dog" ) ) {
    cout << "there is dog";
}
else {
    cout << "there is no dog" << endl;
}

CodePudding user response:

line[0], line[1], and line[2] are chars, not std::strings. char is an integer type, so adding two chars together results in a single integer that is the sum of the two operands. It does not produce a std::string that is the concatenation of the two chars.

To get a substring of a std::string use the substr member function:

std::string search = line.substr(0, 3);

Or, if you actually need to construct a std::string from individual chars, use the constructor that accepts a std::initializer_list<char>:

std::string search{line[0], line[1], line[2]};

CodePudding user response:

A string made from the first characters of line can be obtained via std::substr. In this case I'd actually prefer the constructor that takes two iterators:

std::string first3chars{line.begin(),line.begin() 3};

Take care of lines that are less than 3 characters.

Your code adds the values of three chars. Adding chars via does not concatenate them, and if it would why call std::to_string on the result? char is an integer type and what you see as 321 is the result of adding the number representations of the first 3 characters in line.

CodePudding user response:

Is there a way for you to cast those chars (which appear to be integer type for some reason) into char type once again. Perhaps that ought to resolve the issue in case the "to_string" concatenates those 3 inputs into one; additionally intelli-sense should do the trick of explaining parameter usage and returning value.

CodePudding user response:

The problem with this code is that when you access an element of a string you get a character which is an ASCII number, when you try to sum two characters you are adding their ASCII codes.

In your specific case, as you want sequential characters, the best solution would probably be to use the substr function (documentation) for strings. Otherwise, you would probably need to convert one of the characters to a string and then “add” the other characters to it.

  • Related