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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:47:04+00:00 2026-06-15T09:47:04+00:00

I am working on a Reverse polish notation calculator. I created a method that

  • 0

I am working on a Reverse polish notation calculator. I created a method that will take care of the calculations but there are three lines in my code that are causing an error. After every = an operation is performed and then displayed. I am trying to grab the string from TxtInputBox and convert to integers but it always shows the catch message Please check the input. Then nothing gets calculated or display. I am sure that my first if statement will check for actual integers and avoid the characters. My ultimate goal is to input a formula in rpn format and have the result display in the multiline textbox.

Sample Input 5 6 -=

Code

namespace rpncalc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void RPNCalc(TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox)
        {
            Stack<int> stackone = new Stack<int>();
            stackone.Clear();
            string[] inputarray = TxtBoxInputbox.Text.Split();
            int end = inputarray.Length - 1;
            int numinput;
            int i = 0;

            do
            {
                if(inputarray[i] != "=" && inputarray[i] != "+" && inputarray[i] != "-" && inputarray[i] != "*" && inputarray[i] != "/")  
                {
                    try
                    {
                        numinput = Convert.ToInt32(inputarray[i]);
                        stackone.Push(numinput);
                    }
                    catch
                    {
                        MessageBox.Show("Please check the input");
                    }
                }

                    else if (inputarray[i]== "+")
                    {
                        try
                        {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        }
                        catch
                        {
                        }
                    }

                    else if (inputarray[i]== "-")
                    {
                        try
                        {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        }
                        catch
                        {
                        }
                    }

                    else if (inputarray[i]== "+")
                    {
                        try
                        {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        }
                        catch
                        {
                        }
                    }

                    else if (inputarray[i]== "*")
                    {
                        try
                        {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        }
                        catch
                        {
                        }
                    }

                    else if (inputarray[i]== "/")
                    {
                        try
                        {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        }
                        catch
                        {
                        }
                    }

            }
            while(i < end && inputarray[i]!= "=" && stackone.Count != 0);
            string txtout = TxtInputBox + " " + stackone.Pop().ToString() + Environment.NewLine;
            TxtOutputBox.AppendText(txtout);
            TxtInputBox.Clear();

        }

        private void Btn_Calc_Click(object sender, EventArgs e)
        {
            RPNCalc(TxtInputBox, TxtOutputBox);
        }

    }
}

enter image description here

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

    What are you doing to increment i after each iteration of your do loop? I tried out your code and it seems like i is never incremented. Also, when you catch and run

    catch
    {
        MessageBox.Show("Please check the input");
    }
    

    You could perhaps change it to:

    catch (Exception e) 
    {
        MessageBox.Show(e.ToString());
    }
    

    so you could be sure of just what you’re catching, and why.

    Edit:

    Here’s my version of your code, now working correctly:

    • i is incremented in each iteration
    • Fixed the typo in the minus, multiplication and division operators that made them do addition instead
    • Removed the redundant addition operator
    namespace rpncalc {
        public partial class Form1 : Form {
            public Form1 () {
                InitializeComponent();
            }
    
            private void RPNCalc (TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox) {
                Stack<int> stackone = new Stack<int>();
                stackone.Clear();
                string[] inputarray = TxtBoxInputbox.Text.Split();
                int end = inputarray.Length - 1;
                int numinput;
                int i = 0;
    
                do {
                    if (inputarray[i] != "=" && inputarray[i] != "+" && inputarray[i] != "-" && inputarray[i] != "*" && inputarray[i] != "/") {
                        try {
                            numinput = Convert.ToInt32(inputarray[i]);
                            stackone.Push(numinput);
                        } catch (Exception e) {
                            MessageBox.Show(e.ToString());
                        }
                    } else if (inputarray[i] == "+") {
                        try {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 + store1);
                        } catch {
                        }
                    } else if (inputarray[i] == "-") {
                        try {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 - store1);
                        } catch {
                        }
                    } else if (inputarray[i] == "*") {
                        try {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 * store1);
                        } catch {
                        }
                    } else if (inputarray[i] == "/") {
                        try {
                            int store1 = stackone.Pop();
                            int store2 = stackone.Pop();
                            stackone.Push(store2 / store1);
                        } catch {
                        }
                    }
                }
                while (i++ < end && inputarray[i] != "=" && stackone.Count != 0);
                string txtout = TxtInputBox.Text + " " + stackone.Pop().ToString() + Environment.NewLine;
                TxtOutputBox.AppendText(txtout);
                TxtInputBox.Clear();
    
            }
    
            private void Btn_Calc_Click (object sender, EventArgs e) {
                RPNCalc(TxtInputBox, TxtOutputBox);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently working with a reverse polish notation calculator. It is fully functional
I am working on an app that will show reverse view from camera. For
As the reverse of i.toString(32) is there a better (i.e. working) method to use
I am a novice C# coder and currently working on a Reverse polish notation
I am currently working with Reverse Polish Notation. I have able to perform all
I am currently working on an application that allows reverse geocoding using silverlight +
I was trying to reverse a number in PL/SQL. It's working fine, but when
I am trying to pass the id through reverse. But it's not working. I'm
I have this action script code that is working perfectly but i am try
I'm working on an application that I will soon be publicly distributing. I would

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.