If statement comparing string.at with char not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(inFile){
    while(getline(inFile, line){
        cout << "that";
        for(int a = 3; a < line.length(); i++){
            if(line.at(a) == '!'){
                cout << '.';
            }
            else{
                cout << line.at(a);
            }
        }
        cout << endl;
   }
}

The comparison of line.at(a) and ! for some reason never executes even though there're clearly !'s in the line.
Could you post your full code and what you are trying to do? I see some things but I don't know if they are intentional or not.

I'd also recommend using a debugger and stepping through the code to make sure values are what you think they are.
Last edited on
I can't post my full code due to school policies, but the program's goal is to take in a file, search for exclamation marks and change them to periods. The punctuation marks are not necessarily at the end of every sentence, so you would then have to output the rest of the line which shouldn't be changed; however, the if statement simply does not execute even though cout << line.at(a) is outputting the exclamation mark.
I can't post my full code due to school policies


I understand; however, it is like you go to a doctor with a high fever and cannot answer the questions to the nurse due to "privacy". It is hard to diagnose the problem without the code to know what is your though process. As Zhuge mentions, use the debugger in your compiler. It will show the values. Good luck.
I can't post my full code due to school policies, but the program's goal is to take in a file, search for exclamation marks and change them to periods.

What you can do is post a snippet of code that is fully compilable and reproduces the problem you're experiencing (perhaps in a different context than that specified by your homework requirements.)
Does that for() loop ever end?

Look closely at this line:
for(int a = 3; a < line.length(); i++){

Topic archived. No new replies allowed.