0

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.

Thomas
  • 2,622
  • 1
  • 9
  • 16
  • 2
    What do you need 200 different `std::string`s for? – Yksisarvinen Nov 01 '18 at 23:19
  • 1
    On the first iteration of the loop, `*arr_temp1` is an empty string. On every iteration of the loop, `*arr_temp1 != str1` so the length check in the loop is suspect. – Igor Tandetnik Nov 01 '18 at 23:20
  • That's the size of the dynamic string array. It won't allow me to compile unless I give it a size. Or shoot, that must be the uh, total number of strings huh? – Thomas Nov 01 '18 at 23:20
  • @IgorTandetnik Its pointing to str1, even if I have a string in it, I get an error that is relative to the size of the initialized string. I'm comparing two strings. arr_temp1 and arr_temp inside of those if statements – Thomas Nov 01 '18 at 23:22
  • 1
    There is no pointer to `str1` anywhere in your code. – Igor Tandetnik Nov 01 '18 at 23:22
  • What does *arr_temp1 = str1 do? – Thomas Nov 01 '18 at 23:23
  • 1
    It copies the value of `str1` into `*arr_temp1`, also known as `arr_temp1[0]` – Igor Tandetnik Nov 01 '18 at 23:24
  • 2
    It seems you could use [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Your program is not doing what you think it's doing. – Yksisarvinen Nov 01 '18 at 23:25
  • 1
    Also, uses a debugger. It help a lot to trace a program line by line to learn what it does. – Phil1970 Nov 01 '18 at 23:39
  • Why do textbooks and colleges insist on teaching students to use deprecated headers? If your instructor is telling you to use string.h and time.h rather than and , I'd drop the class based on that alone. – Christopher Pisz Nov 01 '18 at 23:45
  • I'm guessing it's a high school course. – Brandon Lyons Nov 01 '18 at 23:52
  • Totally unrelated: When I was a kid I read a comic and story anthology my mom would bring home from work. Huge portions of the book always seemed to be written by a "Jovial Bob Stine". I was 40 before I found out he was the R L Stine scaring the crap out of the kids. – user4581301 Nov 01 '18 at 23:53

1 Answers1

3
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.

Brandon Lyons
  • 473
  • 3
  • 10
  • There is another error with using a y value for the str.compare(y, 1, str2, x, 1). So I just hard coded it for now. It wasn't an error, it just crashed. – Thomas Nov 05 '18 at 03:31