Reputation:
I am a novice C# coder and currently 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 the following line is where I am getting a red squiggly line: string[] inputarray = TxtBoxInputbox.Text.Split();
. Second when calling the function RPNCalc from button click Btn_Calc
and assigning the textBoxes I receive another red squiggly line: RPNCalc(TxtInputBox, TxtOutputBox);
I am not sure how to deal with these two errors that keep my form from working. Please help in making error free.
Code
namespace rpncalc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void RPNCalc(TxtBoxInputbox, 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);
}
}
}
Upvotes: 0
Views: 188
Reputation: 116458
Your RPNCalc
method signature is missing the types for the parameters. Did you mean:
private void RPNCalc(TextBox TxtBoxInputbox, TextBox TxtBoxOutputbox)
Side note, as RPNCalc
is a member function of the form, it already has access to members this.TxtBoxInputbox
and this.TxtBoxOutputbox
. So you do not actually need to pass it any parameters at all.
While we are on the topic, if it were my code, I would probably rewrite the whole method with no parameters, to return an int (i.e. private int RPNCalc()
) which would take whatever is in this.TxtBoxInputbox
, do calculations, and return the result. The calling method could then do what it wants with the result (in this case, adding it to the output). However, this does go beyond the scope of the original question.
Upvotes: 1