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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:16:49+00:00 2026-06-06T09:16:49+00:00

So, I have the following (kludgy!) code for an infix to postfix expression converter

  • 0

So, I have the following (kludgy!) code for an infix to postfix expression converter and calculator (as I mentioned on my previous post: Simple numerical expression solver, thanks to everyone!):

#include <iostream>
#include <string>
#include <stack>

using namespace std;

int main()
{
    stack<char> operators;  
    stack<char> output;
    stack<char> temp;       
    stack<char> answer; 

    string command;

    cout << "=>";
    cin >> command;

    // "Shunting Yard" algorithm
    // source: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
    for(int i=0; i<command.size(); i++)
    {
        switch(command[i])
        {
        case '*': case '+': case '-': case '/': case'(': 
            operators.push(command[i]);
            break;

        case ')':
            while(operators.top() != '(')
            {
                output.push(operators.top());
                operators.pop();
            }
            operators.pop();
            break;

        default:
            output.push(command[i]);
            break;
        }
    }

    while(!operators.empty())
    {
        output.push(operators.top());
        operators.pop();
    }

    while(!output.empty())
    {
        temp.push(output.top());
        output.pop();
    }

    while(!temp.empty())
    {
        if(temp.top() == '+')
        {
            int a = atoi(&answer.top());
            cout << "A=" << a << endl;
            answer.pop();
            int b = atoi(&answer.top());
            cout << "B=" << b << endl;
            answer.pop();
            answer.push(b+a);
        } else {
            answer.push(temp.top());
        }
        temp.pop();
    }

    cout << answer.top() << endl;

    system("pause");
    return 0;
}    

Anyway, the problem is: if I enter, for instance, 3+4, the result is “&”, when the correct result would be “7”. So, what’s wrong with my code?

  • 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-06T09:16:51+00:00Added an answer on June 6, 2026 at 9:16 am

    There are two problems here.

    First:

    int a = atoi(&answer.top());
    

    atoi takes a pointer to a null-terminated string of characters. But &answer.top() is just a pointer to a single character. So atoi is going to start reading from that character, then continue marching through memory until it finds a ‘\0’ character (or a non-digit). Depending on how the stack is implemented on your platform, that may mean it reads the ‘4’, then the ‘3’, then a ‘\0’, so it ends up with “43”. Or it may read the ‘4’, then some uninitialized memory that happens to start with “8675309j”, so it ends up with “48675309”.

    In case you’re wondering why the compiler doesn’t warn you about this error, the problem is that C-style strings and pointers to single characters are syntactically the exact same type (char*), so a compiler can’t tell you’re mixing them up unless it understands the semantics of atoi. This is one of the many reasons that it’s better to use the C++ string class and functions, instead of the C char* based functions.

    Second:

    answer.push(b+a);
    

    b+a is an int, but you’re pushing it into a stack of chars. So, even if it had the right value, you’d be pushing the character ‘\007’, not the character ‘7’. You need to re-stringize it. But in this case, you’ve apparently got something like, say, 305419814, which is truncated to the low 8 bits (38) when cast to char, and 38 is ‘&’.

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

Sidebar

Related Questions

I have following simple code: <%@ Page Language=C# AutoEventWireup=true CodeBehind=testForm.aspx.cs Inherits=Orbs.testForm %> <html> <body>
I have following code in initialization im = imread('Image02.tif'); figure(); imagesc(im); colormap(gray); [hImage hfig
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
I have following code that creates Linq query. I've never used Linq until today
I have following code in C# PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
Have following code: public interface ITest { string St1 { get; } } public
I have following batch file code: @echo off SET INSTALL_PATH=c:\program files\ :ask_again if exist
I have following code for an autocomplete box, I'm adding an image for choice
I have following vb.net code and i am getting syntax error in it Update
I have written the following C# code: _locationsByRegion = new Dictionary<string, IEnumerable<string>>(); foreach (string

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.