# Split Method

Page 1 of 1

## 6 Replies - 748 Views - Last Post: 15 March 2011 - 03:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=221138&amp;s=ba2b194361f73714b3e3a06fd4ef556e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 XDAO

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 09-March 11

# Split Method

Posted 09 March 2011 - 04:45 PM

Hello I am working on a calculator for my class and I am have trouble getting the program to accept a single line of input which will contain the entire equation to be executed. Are teacher said to use the split method but I don't understand it at all. So if anyone could give me an example on how to use the split method with two numbers and a operation, or give me another way to do it all together. I don't see my teacher till Friday and I don't want to waste a day not doing anything to my project.

``` class Program
{
static void Main(string[] args)
{
double num1, num2;
double total = 0;
char operation;
string inputstring;
const char Quit = 'Q';

Console.WriteLine("Enter a number ");
num1 = Convert.ToDouble(inputstring);

Console.WriteLine("Enter a operation ");
operation = Convert.ToChar(inputstring);

Console.WriteLine("Enter another number ");
num2 = Convert.ToDouble(inputstring);

while (operation != Quit)
{

if (operation == '+')
{
total = num1 + num2;

}

else
if (operation == '-')
{
total = num1 - num2;

}
else
if (operation == '*')
{
total = num1 * num2;
}
else
if (operation == '/')
{
total = num1 / num2;

}

num1 = total;

Console.WriteLine("Enter a operation or " + Quit + " to stop ");
operation = Convert.ToChar(inputstring);

Console.WriteLine("Enter another number ");
num2 = Convert.ToDouble(inputstring);

}
Console.WriteLine("Total is {0} ", total);
```

Is This A Good Question/Topic? 0

## Replies To: Split Method

### #2 Momerath

• D.I.C Lover

Reputation: 1012
• Posts: 2,451
• Joined: 04-October 09

## Re: Split Method

Posted 09 March 2011 - 04:50 PM

Since you are entering the numbers and operation in different prompts, I don't see a need for split at all. But if you entered them all in one prompt, with the user placing spaces between each (2 + 3, for example), then split would work for you:
```string myString = "2 + 3";
string[] parts = myString.Split(' ');
```

After this parts[0] would have "2", parts[1] would have "+" and parts[2] would have "3".

### #3 tlhIn`toq

• Please show what you have already tried when asking a question.

Reputation: 5871
• Posts: 12,798
• Joined: 02-June 10

## Re: Split Method

Posted 09 March 2011 - 05:00 PM

XDAO, on 09 March 2011 - 05:45 PM, said:

Hello I am working on a calculator for my class {...}

There's a tutorial for that
Calculator tutorial

Plus
Bulding an application - Part 1
Building an application - Part 2

### #4 tlhIn`toq

• Please show what you have already tried when asking a question.

Reputation: 5871
• Posts: 12,798
• Joined: 02-June 10

## Re: Split Method

Posted 09 March 2011 - 05:19 PM

Learning to find answers is a very import part of coding. We all search and try to find some article or description about what we are about to understand, before we try to actually implement it. It's amazing how much aspirin you can save by reading about something before you go off an try to use it. Not to mention is is a lot faster than formulating a question and waiting for someone else to answer it: Especially if you are like me and code at 0300hrs when most sane people are sleeping.

One of my favorite methods is Google.
In this case doing a search for "MSDN String.Split" gives you very targeted results.
I actually find this better than searching the MSDN through their built-in search feature.

This post has been edited by tlhIn`toq: 09 March 2011 - 05:20 PM

### #5 XDAO

• New D.I.C Head

Reputation: 0
• Posts: 2
• Joined: 09-March 11

## Re: Split Method

Posted 14 March 2011 - 05:28 PM

I don't understand how to make the program I have accept a single line of input which will contain the entire equation to be executed. Any tips to do this or any thing I should look up that might help?

```class Program
{
static void Main(string[] args)
{
double num1, num2;
double total = 0;
char operation;
string inputstring;
const char Quit = 'Q';

Console.WriteLine("Enter a number ");
num1 = Convert.ToDouble(inputstring);

Console.WriteLine("Enter a operation ");
operation = Convert.ToChar(inputstring);

Console.WriteLine("Enter another number ");
num2 = Convert.ToDouble(inputstring);

while (operation != Quit)
{

if (operation == '+')
{
total = num1 + num2;

}
else
if (operation == '-')
{
total = num1 - num2;

}
else
if (operation == '*')
{
total = num1 * num2;

}
else
if (operation == '/')
{
total = num1 / num2;

}

num1 = total;

Console.WriteLine("\n" + total);

Console.WriteLine("Enter a operation or " + Quit + " to stop ");
operation = Convert.ToChar(inputstring);

Console.WriteLine("Enter another number ");
num2 = Convert.ToDouble(inputstring);

}

```

### #6 modi123_1

• Suitor #2

Reputation: 10128
• Posts: 38,814
• Joined: 12-June 08

## Re: Split Method

Posted 14 March 2011 - 06:03 PM

It's all about parsing. Input a single line like "5*40=" as a string, right? Then parse that string any way you see fit. The common method would be to check each spot of the character array and decide what to do. Dump them in an array, a queue, or what ever storage medium you feel is needed.

In this case the first spot is a number. I peek to the next index and see that is *not* a number so I push 5 off to an integer variable.

I move to the second and see it's a plus sign. I save that off as an operator.

The next is a number and I peek to see there's a number after it. I take 44 and put that into another integer variable.

The last sign is an equals so it means calculate up what you have.

I take my sign, run it through a select case to see that int_variable1 and int_variable2 should be added and the result outputted.

Clearly this is a super trivial example but you see how this pans out. It gets more interesting with nested parentheses and so forth.

### #7 Dogstopper

Reputation: 2908
• Posts: 11,151
• Joined: 15-July 08

## Re: Split Method

Posted 15 March 2011 - 03:44 AM

Duplicate Topics merged. Please, if you're going to get help, take our advice and change your code. This code is virtually identical to the one that you posted before. This shows that you didn't really take our advice.