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.