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

  • SEARCH
  • Home
  • 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 6906695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:20:34+00:00 2026-05-27T08:20:34+00:00

I have calculator code where I need to save a given line (one line

  • 0

I have calculator code where I need to save a given line (one line at a time calculator) based on a certain character like ‘M’ being present at the end of the string. Also if ‘M’ is anywhere in the string then add the last saved string in place of that ‘M’.

I thought this could be easily done by (pseudo code),

foreach input element
  if input element == 'M' && is last element
    save = true
    erase M element

  if input element == 'M' && save != true
    insert saved string into input
    erase 'M' from input

  if save == true
    Set save string = input;

I attempt to do this in the method below but it is not working. I am not sure where or how to save the string so that it doesn’t get reset on every line. I get “string subscript out of range” (in another class, but due to this change) when I try to use an ‘M’ at the end of input.

const string operators = "*/+-";
const string memorize = "M";

list<string> lex(string input)
{
  list<string> tokens;
  bool save = true;
  string saveInfo = "2";
  string token;

  for (unsigned int i = 0; i < input.length(); i++)
  {
    // Check if we need to save string element to memory
    if ((memorize.find(input[i]) != string::npos) && (i == input.length() - 1))
    {
      save = true;
      // Remove 'M' from end of string
      input.erase(i);
    }

    // Check if 'M' is element
    if (memorize.find(input[i]) != string::npos && !save)
    {
      // Insert saved string
      input.insert(i, saveInfo);

      // Remove M from string
      input.erase(i);
    }

    // Save input
    if (save)
    {
      saveInfo = input;
    }

    if (operators.find(input[i]) != string::npos)
    {
      // Add any token we've created so far
      if (token.length())
      {
        tokens.push_back(token);
        token.clear();
      }

      // Add this operator as a separate token
      token.push_back(input[i]);
      tokens.push_back(token);
      token.clear();
    }
    else
    {
      // Grow the current token
      token.push_back(input[i]);
    }
  }

  // Any stragglers?
  if (token.length())
  {
    tokens.push_back(token);
  }

  // Clean 'em up
  for (list<string>::iterator i = tokens.begin(); i != tokens.end(); ++i)
  {
    *i = clean(*i);
  }

  return tokens;
}

saveInfo is currently just being set to “2” every time until I figure out where my string variable should be to keep the saved string instance each time.

Is this the right way to go about this problem? Is there a better way? Do you know why this isn’t working?

EDIT:

example input:

INPUT: 2+1M
OUTPUT: 3  **2+1 is saved
INPUT: M+5  **2+1 is inserted into this input string at 'M'
OUTPUT: 8

I have not worked with C++ in a long time, any help is appreciated!

  • 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-05-27T08:20:35+00:00Added an answer on May 27, 2026 at 8:20 am

    First thing that comes to mind is this: you’re iterating over a sequence while you modify it by adding and removing items. that’s bound to cause tears since your indices are wrong after your insertion/deletion.

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

Sidebar

Related Questions

I have a win form UI that looks like a typical calculator. Naturally I
I have this c code, where i need to calculate a dobule from a
I have created a nice scientific calculator and now want to save inputs and
I need some help with floating point numbers...please! Here's the thing, I have code
I have code to calculate the percentage difference between 2 numbers - (oldNum -
I have a fortran (unmanaged code) dll as the calculation engine, and a C#
I have a small calculator that I am creating in C# (Sharp Develop). The
I have implemented a file transfer rate calculator to display kB/sec for an upload
I have the following text input on a budget calculator form which displays the
I have been running this code (from: http://blog.somethingaboutcode.com/?p=155 ): from twisted.internet import reactor from

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.