Thomas
Thomas

Reputation: 2962

Program crashing with 'std::out_of_range' error

I am working on a wheel of fortune project for school and am running into some issues with pointers.

This is the issue I have on my program, (cmd output):

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::compare: __pos (which is 1) > this->size() (which is 0)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

The game is designed to work similarly to the wheel of fortune game. What I'm doing at first is filtering out the 'rlstne' letters. That works without using pointers, but I have to use a pointer. This is my full program:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <cctype>
#include <time.h>
#include <Windows.h>

int main(){

    std::string original_string = "THIS LINE IS THE GAME";
    std::string str1;
    std::string str2 = "RLSTNE";
    int y = 0;

    bool restart = true;

    std::string* arr_temp =  new std::string[100];
    std::string* arr_temp1 = new std::string[100];
    *arr_temp1 = str1;
    *arr_temp = str2;
do{

        std::cout << "What string?" << std::endl;
        getline(std::cin, str1);
        std::cout << str1.length() << std::endl;

    for(int x = 0; x < str1.length(); x++){

        if((arr_temp->compare(0,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(1,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(2,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(3,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }   
        if((arr_temp->compare(4,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
        if((arr_temp->compare(5,1, *arr_temp1, x, 1)) == 0){
            str1[x] = '_';
        }
}
*arr_temp1 = str1;
std::cout << *arr_temp1 <<std::endl;
Sleep(1000);
}while(restart);
}

I think this is where my program goes wrong:

std::string str1;
std::string str2 = "RLSTNE";

str1 is not initialized to any value, so the compiler sees it as 0 length, but I've tried initializing it to different values. Such as the string value of the original_string.

This is the code:

std::string str1 = "THIS LINE IS THE GAME";
std::string str2 = "RLSTNE";

This is the output:

What string?
THIS LINE IS THE GAME
21
_HI_ _I__ I_ _H_ GAM_

But when I try to add more than the original value which is 21, I get this problem:

What string?
THIS LINE IS THE GAMEEEE
24
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::compare: __pos (which is 22) > this->size() (which is 21)

So my question is: What is the compiler printing out? What value is 22, what value is 21? What does this->size mean? what does __pos mean?

Thanks in advance.

Upvotes: 0

Views: 357

Answers (1)

Brandon Lyons
Brandon Lyons

Reputation: 463

std::string* arr_temp =  new std::string[100];
std::string* arr_temp1 = new std::string[100];

Each of these is a pointer to an array of 100 strings. Also, because you use new, you need delete somewhere in your code, otherwise you've got a memory leak. But you don't seem to need dynamic memory. So the fixed version would be

std::string* arr_temp;
std::string* arr_temp1;

Your big for loop can be simplified with a nested loop, but that's not really the focus here. As for your error - the exception std::out_of_range means you went past the limits of your array. The code causing it is as follows:

std::string str1 = "THIS LINE IS THE GAME"; //makes a new string
*arr_temp1 = str1; //dereferences arr_temp1 and sets arr_temp1[0] to whatever str1 currently is

So you've set arr_temp1[0] = "THIS LINE IS THE GAME" (length of 21). Then you set str1 = "THIS LINE IS THE GAMEEEE" (length of 24). Your loop tries to access the first 24 characters of arr_temp1[0]. But that won't work - it has a length of 21. So as soon as it gets to the 22nd character, it throws you an std::out_of_range error.

In summary, most of this isn't doing what you think it is. I recommend reading up on pointers a bit and start from scratch.

Upvotes: 3

Related Questions