0

Okay so I am trying to make a c# chat program, you can chat with a bot (it is supposed to be a Virtual Assistant program.). I am making a calculator, but it won't work. This is my code:

reply(text.Split(' ')[1]);
int i = int.Parse(text.Split(' ')[1]);
reply(i.ToString());

reply is a void, I am using a listbox for the chat and reply adds the text to the listbox like "Bot: -string-". So reply("a") will add "Bot: a" to the listbox. Okay so this is not working, when I am writing calculate 5+5. And I am sure that text.Split(' ')[1] is 5+5.

I tried something like this:

int i = 5+5;

and it worked, I don't understand why it is not working with my code. I haven't seen a calculator made this easy, and I don't know if this is possible to do it like this. Thank you for help!

2
  • Your question is very difficult to understand, in its completeness. There are lot of unwanted details, but not much about what is the issue you face with the integer parsing, or if that is what you even need. Commented Mar 20, 2019 at 16:24
  • I am sorry. I am currently learning English and also learning programming. I give the details so I don't need to write extra comments to write more details. Also I don't think my question is difficult to understand. Commented Mar 20, 2019 at 16:33

3 Answers 3

3

It won’t work, int parse only recognizes numbers not symbols. You might want to figure out a better way to do it.

You can however use the following expression:

using System.Data;
DataTable dt = new DataTable();
var v = dt.Compute("3 * (2+4)","");

That’s an example. Hope it works for you, of course, you just have to change the string **3 * (2+4) ** for your string, or your variable.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use DataTable.Compute trick:

string text = "calculate 5+5";
int i = (int)new DataTable().Compute(text.Split(' ')[1], null);

Comments

0

The String "5+5" can not be parsed by int.Parse you need to implement some form of evaluation code, maybe this question can help you Evaluating string "3*(4+2)" yield int 18

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.