String.at() not seem to be working?

I'm trying to find a char in a string but the loop never executes
1
2
3
4
5
6
7
8
for(int a = 3; a < line.length(); a++){
    if(line.at(a) == 'g'){
        //do something
    }
    else {
        cout << line.at(a);
    }
}

The if loop never executes, and only the original string is printed. What am I doing wrong?
Last edited on
The if loop never executes


Are you sure about that?

"if" is not a loop.

The body of the for loop will never be executed if line.length() >= 3
Otherwise, if line does not contain 'g', then the if branch will not be executed.
The condition will still be tested every time through the loop.
If there is no 'g' then you'll get the behaviour described, the else branch will be executed each time and the result is displaying your original string.

Can you show a working example, along with input and output?
The input string is "that cag moved"
a starts at 3 because every string starts with "that". I'm trying to correct every "g" into a "t", but every time, I only print out "that cag moved". The if statement never executes.
Ok, I guess I should add that I have the for loop inside
1
2
3
4
5
6
7
8
if(inFile){
    while(getline(inFile, line){
        if(line.substr(0, 3) == "that"){
            cout << "that";
            for(......){
                

            }

Maybe it's a problem with the stuff before it?
Topic archived. No new replies allowed.