#Why does my program run all the way through normally, but throw 'std out_of_range' once its done?

59 messages · Page 1 of 1 (latest)

olive sonnet
#

Hi,
So I am a little confused about why my program is running the way it is right now. I just made a helper function that looks for stop words, and if it does find one... it removes that stop word.

Well, the program seems to run through completely and take out every stop word, but for some reason once it gets to the end it throws a out_of_range error! Does anyone know why this might be? I have a feeling it is because it goes past the last line and that throws the error? but im not sure... any advice would be appreciated :)

here is my function:

//helper function that only outputs non helper words
    string strStop(string str) {
        string text;


        ifstream file;
        file.open("stop.txt");

        while (getline(file, text)) {

            while (str.find("text") != string::npos) {

                str.replace(str.find(text), str.length(), "");
            }
        }
        return str;
    }```

and here is my error:
```terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace: __pos (which is 4294967295) > this->size() (which is 32)```

How do I fix this?
warm pierBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

cedar sparrow
#

Run it under a debugger so it shows you where that happened and can inspect that values of the various variables.

#

Also str.find("text") should probably be str.find(text).

olive sonnet
#

how do i use a debugger? i am using vscode right now

cedar sparrow
#

Ideally you click this and it just works:

olive sonnet
#

hmm... when i press it, it just does this

#

and the debug anyway option just doesnt work haha

cedar sparrow
#

I'm not sure. I don't use VSC for C++.

#

Something something install the correct plugins and have the correct launch options.

olive sonnet
#

ugh i hate how complex launch options can be

worthy violet
worthy violet
olive sonnet
#

but now how do i run my programs in it?

cedar sparrow
#

You open the project and press F5.

#

Make sure to open the project, not just a file.

worthy violet
olive sonnet
#

so what do i do if i only have files and not a project?

#

should i just make a project and put my files in it?

worthy violet
#

yes

olive sonnet
#

okay, i think im slowly getting it figured out. it is saying that i can't open a source file even though it is in the folder. why is that?

worthy violet
#

🤷

#

vs is easy but not zero effort

#

frankly just start over from scratch and manually copy paste your old code over from VSCode

#

right click all the things

olive sonnet
#

yeah i did, i just am having problems since i have more than one file, and for some reason my main.cpp file isn't finding the other files on vs

worthy violet
#

it really shouldn’t be too difficult to figure out

worthy violet
#

copy paste

#

also I would re emphasize

worthy violet
olive sonnet
#

what does it mean cryingmichaeljordan

#

bruh

#

okay also now everything is working and runs

#

its now saying (process 7896) exited with code 0. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

#

instead of showing me the output... im so confused lmao i just wanna know why it goes out of range

cedar sparrow
#

It's saying the program completed successfully. There was no out of range error.

#

And it's telling you how to keep the console open after the program closes if you care to see the output.

#

Alternatively press CTRL+F5 which runs it without the debugger and without closing the console.

olive sonnet
#

why isn't it showing an output of my program? and why doesn't it say there is an out of range error, if i didn't change anything from vscode to vs?

cedar sparrow
#

Because by default it doesn't keep the console open and I guess now they keep that setting because people are used to it.

#

And my best guess why the out of range error is gone is that Visual Studio uses the build directory as the current working directory, so it failed to find stop.txt

#

Use an absolute path or set the current working directory to where the file is.

olive sonnet
#

is there a setting or something i can do to keep the console open? but that would make sense. i set the absolute path but it is still saying it exited with code 0

cedar sparrow
#

Dude, it literally told you how to do it in the message you posted.

olive sonnet
#

bruh

#

thats to close the console when its done

olive sonnet
fresh oak
worthy violet
#

thanks, yes it is

worthy violet
#

older versions of VS did not set the "stay open" option by default, but newer ones should

#

I recommend trying the default console app starter project template. like create a new one, don't change it, and just click the "Play" button (green triangle), you should get a console that stays open and says "Hello World"

#

that way you can confirm it does what you want, then you can investigate why your project is behaving differently

warm pierBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.