I have a misfortune though. At some point in the game the player can enter his name. So, from that point on I want the texts to have the player's name.
The thing is, to have a much simpler code and more freedom in creating the game, I read the texts from external texts files. That means I read each line of the file and then print it on the screen.
So what I thought is create a sequence of characters that I can read and replace them in the program.
So I came up with the "/NAME/" sequence. So, the player enters his name, "Nick" for example. The program should replace the "/NAME/" sequence with "Nick" and present the rest of the text normally.
So I wrote this code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <fstream> #include <sstream> using namespace std; int main (){ string line; char name[20]; fstream file; file.open("Rebels_after_start.txt"); cin >> name; while(file.good()){ getline(file,line); line.replace(line.find("/N"),line.find("E/"), name ,6); //line.replace(19,1,1,'s'); Even with this line it crashes cout << line << '\n'; } file.close(); }
But the program keeps getting an out_of_range instance from the replace function. I've tried all iterations of string.replace and nothing works. Even if I replace a character with a character the program crashes.
Any help please?