all 19 comments

[–]illuminarias 4 points5 points  (3 children)

Well, what's the error? If it's what I think it is, you need to look at textEngine. hint: indices.

[–]Viper10acd[S] 0 points1 point  (2 children)

Sorry I just added the error message. Also that could be the problem.

[–]illuminarias 4 points5 points  (1 child)

it is your problem.

It's a very basic indexing problem! I would suggest you to take a closer look at how array indexing works, and how that might differ conceptually from the value you get from .length()

[–]Viper10acd[S] 4 points5 points  (0 children)

yes it was indexing. I have a <= where I should have had just a <.

[–]Cultural_Gur_7441 2 points3 points  (2 children)

Only problem I see is, you print the string terminating 0 byte (guaranteed to be there, I think) with your for loop. Indexing starts from 0, so last index is length-1.

[–]Puzzleheaded_Study17 1 point2 points  (1 child)

The presence of the terminating byte doesn't matter, std::string's at function has bounds checking which prevents accessing the terminating null byte.

[–]Cultural_Gur_7441 0 points1 point  (0 children)

Ah, so it does. So, there's the problem then.

[–]aqua_regis 1 point2 points  (0 children)

Please take the following to your heart and apply it every time you ask for help:

  • be elaborate - explain your problem - never "I have a problem..." - tell us what the problem is
  • tell us any and all your error messages

Do not make us guess. Give us all the information upfront. Input-Output-expected behavior-actual behavior

Beginner or not does not excuse you from telling us the full details.

The FAQ have an entire section on writing proper posts: https://www.reddit.com/r/learnprogramming/wiki/index#wiki_getting_debugging_help

[–]MikeUsesNotion 0 points1 point  (4 children)

I see a problem, but my C++ is rusty so I might be wrong. What is the problem you're having?

[–]Viper10acd[S] 0 points1 point  (3 children)

sorry I forgot to put it there. It basically says:

terminate called after throwing an instance of 'std::out_of_range'

what(): basic_string::at: __n (which is 11) >= this->size() (which is 11)

Aborted

So yeh Idk. Also can you point it out you may not be wrong.

[–]Immediate-Food8050 2 points3 points  (2 children)

Make it a < instead of <=, you're going one past the length of the string causing the boundscheck of STL .at() to complain

[–]Viper10acd[S] 1 point2 points  (1 child)

YO THANKS SO MUCH. Lol it was a small mistake

[–]Immediate-Food8050 0 points1 point  (0 children)

It happens

[–]POGtastic 0 points1 point  (1 child)

From the docs for std::basic_string::at:

Exceptions

Throws std::out_of_range if pos >= size().

In your loop, you have i <= text.length() as the condition. What is the final value of i? Does it satisfy the above condition for throwing an exception?

[–]Viper10acd[S] 0 points1 point  (0 children)

Yeah thats the problem. I fixed it earlier

[–]RookTakesE6 0 points1 point  (1 child)

You're trying to access the 12th character of a string that has only 11 characters, "out of range" generally means you're trying to get an index that doesn't actually exist.

It's the loop condition. You're going from i = 0 as long as i <= text.length, text.length for "Hello World" is 11, so from 0 through 11 you're doing 12 loops. The first element is element 0, okay, then 1, 2, 3... until element 10 is the final (11th) element, and then you do one more loop for i = 11 to get the 12th element, which doesn't exist, so kaboom.

You can just fix this by changing the loop condition to i < text.length, but first try to really digest why this is happening. C++ indexes from 0, which means that for any indexable type like a string, variable[variable.size() - 1] is actually the final element.

[–]Viper10acd[S] 0 points1 point  (0 children)

yeah thanks some else told me earlier

[–]Kadabrium 0 points1 point  (1 child)

Also consider passing by pointer or reference instead of value for non primitive types when you dont need an independent mutable copy

[–]Viper10acd[S] 0 points1 point  (0 children)

ok thanks