Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 879929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:00:54+00:00 2026-05-15T12:00:54+00:00

First, thank for you all your help! The error I am getting is: Unhandled

  • 0

First, thank for you all your help!

The error I am getting is:

Unhandled exception at 0x7c812afb
(kernel32.dll) in Readerboard.exe:
Microsoft C++ exception:
std::out_of_range at memory location
0x0012f8a8..

I have found the problem to be with this line:

str.replace(str.find(sought), sought.size(), replacement);

It is located in this procedure:

void DisplayMessages() {

 ifstream myReadFile;
 string str;
 static string myMessages[10];
 static int i; // of course my famous i
 static int MyPosition;
 string sought;
 string replacement;

 myReadFile.open("C:\\Documents and Settings\\agerho000\\Desktop\\cms_export_test\\outages.htm",ios::in);
 i = 0; //the start of my array
 sought = "</td>"; // value that I want to replace with nothing
 replacement.clear();

 if(!myReadFile) // is there any error?
{
    cout << "Error opening the file! Aborting…\n";
    exit(1);
}

 if (myReadFile.is_open()) 
 {
    cout << endl;
    while (!myReadFile.eof()) 
    {
    getline(myReadFile, str);

    if (str == "<tr>")
    {       
        myReadFile.seekg(4,ios::cur);
        getline(myReadFile, str);
        str.replace(str.find(sought), sought.size(), replacement); 

        cout << str;

        myMessages[i]=str;
        i++;
    }

    }

}

i=0;
while (i < 10)
{
    cout << i << ") " << myMessages[i] << endl;
    i++;
        if (myMessages[i]=="")
        {
            break;
        }
} 

myReadFile.close();

mainMenu();
}

The whole cpp file is displayed below:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void mainMenu();
void DisplayMessages();
void AddMessage();
void DeleteMessage();
void EditMessage();
void RunTests();
void CheckFile();
void CreateHtmlFile(string myMessages[10]);
/*
#define MIN     1
#define MAX     100

#define TRUE    1
#define FALSE   0
*/

int main() {
    cout << endl;
    cout << endl;
    cout << "Hello Andrew.\n";
    cout << "First you need some sort of menu.\n";

    mainMenu();

    return 0;
}


void mainMenu() {

    int Command;

    cout << endl;
    cout << endl;
    cout << endl;
    cout << "What would you like to do?\n";
//  cout << "1) Check that tests work!\n";
//  cout << "2) Check that the file exists\n";
    cout << "3) Display Messages\n";
//  cout << "4) Edit a message\n";
//  cout << "5) Add a message\n";
//  cout << "6) Delete a message\n";
    cout << "7) Exit\n";
    cout << "Enter a number: ";
    cin >> Command;

    if (Command == 3)
    {
        DisplayMessages();
    }

    if (Command == 7)
    {
        cout << "Exiting...";
        exit(EXIT_SUCCESS);
    }

    if (Command == 6)
    {
        DisplayMessages();
    }
}


void DisplayMessages() {

 ifstream myReadFile;
 string str;
 static string myMessages[10];
 static int i; // of course my famous i
 static int MyPosition;
 string sought;
 string replacement;

 myReadFile.open("C:\\Documents and Settings\\agerho000\\Desktop\\cms_export_test\\outages.htm",ios::in);
 i = 0; //the start of my array
 sought = "</td>"; // value that I want to replace with nothing
 replacement.clear();

 if(!myReadFile) // is there any error?
{
    cout << "Error opening the file! Aborting…\n";
    exit(1);
}

 if (myReadFile.is_open()) 
 {
    cout << endl;
    while (!myReadFile.eof()) 
    {
    getline(myReadFile, str);

    if (str == "<tr>")
    {       
        myReadFile.seekg(4,ios::cur);
        getline(myReadFile, str);
        str.replace(str.find(sought), sought.size(), replacement); 

        cout << str;

        myMessages[i]=str;
        i++;
    }

    }

}

i=0;
while (i < 10)
{
    cout << i << ") " << myMessages[i] << endl;
    i++;
        if (myMessages[i]=="")
        {
            break;
        }
} 

myReadFile.close();

mainMenu();
}

void AddMessage() {
}
/*
void DeleteMessage() {
 ifstream myReadFile;
 string str;
 static string myMessages[10];
 static int i; // of course my famous i
 static int MyPosition;
 string sought;
 string replacement;
 static int Command;

 myReadFile.open("C:\\Documents and Settings\\agerho000\\Desktop\\cms_export_test\\outages.htm",ios::in);
 i = 0; //the start of my array
 sought = "</b></td>"; // value that I want to replace with nothing
 replacement.clear();

 if(!myReadFile) // is there any error?
{
    cout << "Error opening the file! Aborting…\n";
    exit(1);
}

 if (myReadFile.is_open()) 
 {
    cout << endl;

    while (!myReadFile.eof()) 
    {
        getline(myReadFile, str);

        if (str == "<tr>")
        {       
            myReadFile.seekg(7,ios::cur);
            getline(myReadFile, str);
            str.replace(str.find(sought), sought.size(), replacement); 

            myMessages[i]=str;
            i++;
        }

    }

}

i=0;
while (i < 10)
{
    cout << i << ") " << myMessages[i] << endl;
    i++;
        if (myMessages[i]=="")
        {
            break;
        }
} 
myReadFile.close();

cout << "Enter the number of the message you would like to delete?\n";
cout << "Or enter 11 to go back to the main menu.\n";
cin >> Command;

while (Command >= 12)
{
    cout << "Invalid number, try again!\n";
    cout << endl;
    cout << "Enter the number of the message you would like to delete?\n";
    cout << "Or enter 11 to go back to the main menu.\n";
    cin >> Command;
}

if (Command == 11)
{
    mainMenu();
}

myMessages[Command].clear();
//clear the string
//now rebuild the htm file with the new array
CreateHtmlFile(myMessages);
}

void EditMessage() {
}

void RunTests() {
}

void CheckFile() {
}

void CreateHtmlFile(string myMessages[])
{
}

                    //File.seekg(-5); moves the inside pointer 5 characters back
                    //File.seekg(40); moves the inside pointer 40 characters forward
                    //tellg() Returns an int type, that shows the current position of the inside-pointer for reading
                    //tellp() same as above but for writing
                    //seekp() just like seekg() but for writing
*/

Please help I am so stumped!

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:00:55+00:00Added an answer on May 15, 2026 at 12:00 pm

    str.replace(str.find(sought), sought.size(), replacement); is wrong when str.find() doesn’t find what it’s looking for. str.find() will return str::npos, which will not be a valid location in the string. Hence, the call to replace fails with the index out of range exception you’re seeing.

    Change that to:

    std::size_t foundIndex = str.find(sought);
    if (foundIndex != str.npos)
        str.replace(foundIndex, sought.size(), replacement);
    else
        std::cout << "Oops.. didn't find " << sought << std::endl;
    

    and let us know if that helps you.

    EDIT: You might also want to consider using boost::algorithm::replace_all from the Boost String Algorithms Library

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi first I like to thank you all for your help We have to
First of all thank you for your time and sorry if some issues are
First, I would to thank everyone for all the help they provide via this
first of all thanks for taking your time! I'm a junior Dev, working with
First and foremost, thank you all for reading this and helping, I'm very grateful.
First, thanks for all the help I've received so far from StackOverflow. I've learned
First of all thank you for reading this. I am having some trouble fetching
When getting all values from db table, I get this error on the last
To all the jQuery guru's out there I could use your help pointing out
Edit: Now fixed! Thank you for your help! Sorry for the beginner questions, I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.