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 8614173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:03:39+00:00 2026-06-12T05:03:39+00:00

I’m getting this error on my program and I don’t understand why. The code

  • 0

I’m getting this error on my program and I don’t understand why. The code essentially has to check for tags stored in a set which is declared as a global variable. If it’s a valid tag it stores it in the stack if not returns a error message. Then it checks (if its a valid tag)if the closing tags are in order. This is all for the is_well_formed method. For the print_well_formed_file method it essentially checks if the given file is well formed if it is it’ll display the file.:

terminate called after throwing an instance of 'std::out_of_range'
what():  basic_string::substr

What can I do to fix this error ?
This is part of the code:

bool is_well_formed(ifstream& ifs, string& error_msg) {
    // your code goes here
    string fname, line;
    Token tok;
    Lexer lexer;
    tags.insert("blue");
    tags.insert("red");
    tags.insert("cyan");
    tags.insert("white");
    tags.insert("yellow");
    tags.insert("magenta");
    tags.insert("dim");
    tags.insert("underline");
    tags.insert("bold");
    while (getline(cin, fname)) {
        // tries to open the file whose name is in string fname
        string name = fname.substr(1, fname.length() - 2);
        cout << "Name" + name;
        ifs.open(name.c_str());
        if (ifs.fail()) {
            cerr << "ERROR: Failed to open file " << fname << endl;
            ifs.clear();
        } else {
            while (getline(ifs, line)) {
                lexer.set_input(line);
                while (lexer.has_more_token()) {
                    tok = lexer.next_token();
                    string tmpTok = tok.value;
                    switch (tok.type) {
                    case TAG:
                        // If it has /, remove / from tmpTok
                        if (tok.value[0] == '/') {
                            tmpTok = tmpTok.substr(1, tmpTok.length() - 1);
                        }
                        if (tags.find(tmpTok) == tags.end()) {
                            // Check whether the encountered tag is valid
                            error_return("Tag " + tmpTok + " is invalid!");
                        } else {
                            // Valid Tag encountered
                            stack < string > tagstack;
                            tagstack.push(tmpTok);
                            // Check if the tags are formed properly
                            if (tok.value[0] == '/') {
                                // Remove / from tmpTok
                                string closingTag = tmpTok;
                                string openingTag = tagstack.top();
                                tagstack.pop();
                                if (closingTag.compare(openingTag) != 0) {
                                    error_return(
                                            closingTag + "doesn't match"
                                                    + openingTag);
                                } //else 
                                //  return true; // if the file is well formed
                            }
                        }
                        break;
                    case IDENT:
                        cout << "IDENT: " << tok.value << endl;
                        break;
                    case ERRTOK:
                        error_return("Syntax error on this line\n");
                        //cout << "Syntax error on this line\n";
                        break;
                    case ENDTOK:
                        break;
                    }
                }
            }
        }
    }
    return true; // if the file is well-formed
}

void print_well_formed_file(ifstream& ifs) {
    //Check if file is well formed.
    string line;
    Lexer command;
    if (is_well_formed(ifs, line)) { //if well formed display
        command.set_input(line);
        display(command);
    }

}
void display(Lexer cmd_lexer) {
    string file_name;

    if (!parse_input(cmd_lexer, file_name)) {
        error_return("Syntax error: display <filename>");
        return;
    }

    ifstream ifs(file_name.c_str());
    string error_msg;
    if (ifs) {
        if (!is_well_formed(ifs, error_msg)) {
            error_return(error_msg);
        } else {
            ifs.clear(); // clear EOF flag
            ifs.seekg(0, ios::beg); // go back to the very beginning
            print_well_formed_file(ifs);
        }
    } else {
        error_return("Can't open " + file_name + " for reading");
    }
    ifs.close();
}

Example of user input:

validate <file name>
display <file name>
exit
  • 1 1 Answer
  • 0 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-06-12T05:03:41+00:00Added an answer on June 12, 2026 at 5:03 am
    string name = fname.substr(1, fname.length() - 2);
    

    Will throw this kind of an exception if fname‘s length is <= 1 . I bet, this is the case. The simplest(not the best) solution is to skip such lines.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an array which has BIG numbers and small numbers in it. I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.