10 Replies - 5801 Views - Last Post: 12 May 2010 - 10:15 PM Rate Topic: -----

#1 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

using split within an array

Posted 12 May 2010 - 05:25 PM

I am writing code for a simple calculator in Visual Studio. I am having a problem grasping how to use a Split in my application. I am new to this and have done well so far learning the basics of if statements, and loops. Any help with just an example of it's use in a simple form would be helpful.Everything I find uses words that I have not heard of or had a chance to study yet and my school book says nothing about it, yet my teacher wants me to use it my code and is of no help explaining how.

Is This A Good Question/Topic? 0
  • +

Replies To: using split within an array

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: using split within an array

Posted 12 May 2010 - 06:58 PM

The split method in C# allows you to define a character to use to split a string into an array of sub strings.

say...

string text = "one,two,three,four,five";
string[] words = text.Split(','); // creates an array containing ["one", "two", "three", "four", "five"]
                                  //  by splitting the string using a comma to separate it(this could be
                                  //  any character though

foreach (string s in words)
{
    Console.WriteLine(s);
}


Was This Post Helpful? 1
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: using split within an array

Posted 12 May 2010 - 07:00 PM

Here's the documentation on string.Split

Here's an example on using it to split on the spaces in a sentence
private void SplitExample()
{
    string example = "This is an example on using the split method";
    string[] words = example.Split(new char[]{' '});
    foreach(string word in words)
    {
        Console.WriteLine(word);
    }
}


Was This Post Helpful? 1
  • +
  • -

#4 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: using split within an array

Posted 12 May 2010 - 07:47 PM

Thanks for the examples. I believe I have a better understanding of the split now but I am still having problems with my array. I am trying to build a calculator and I don't have any error messages but my command prompt wont stay on when I am trying to test. Here is my code I have written so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_2_2
{
    class Program
    {
        static void Main()
        {
            double num1;
            double num2;
            double total;
            string [] equation = { num1 + num2, num1 - num2, num1 * num2,num1 / num2};
            string inputString = Console.ReadLine;
            string input = inputString.Split(',');
            
            const string QUIT = "quit";
            Console.ReadLine();

            Console.WriteLine("Enter equation");
            equation[4] = Convert.ToDouble(Console.ReadLine());
            

            while (equation.Length != 0)
           
            {
                if (equation[4] = QUIT)
                    Console.WriteLine("GoodBye");
                     
 
                while (oper != QUIT)
                    
                {
                        

                        switch (equation)
                        {
                            case equation[0]:
                                total = num1 + num2;
                                Console.WriteLine("{0} {1} {2} = {3}", equation[0], total);
                                num1 = total;
                                break;
                            case "-":
                                Console.WriteLine("Enter number");
                                num2 = Convert.ToDouble(Console.ReadLine());
                                total = num1 - num2;
                                Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                                num1 = total;
                                break;
                            case "*":
                                Console.WriteLine("Enter number");
                                num2 = Convert.ToDouble(Console.ReadLine());
                                total = num1 * num2;
                                Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                                num1 = total;
                                break;
                            case "/":
                                Console.WriteLine("Enter number");
                                num2 = Convert.ToDouble(Console.ReadLine());
                                total = num1 / num2;
                                Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                                num1 = total;
                                break;
                            case "=":
                                
                                Console.WriteLine("Enter number");
                                num1 = Convert.ToDouble(Console.ReadLine());
                                
                          
                                break;
                            case "QUIT":
                                Console.WriteLine("Thank You for using my calculator!");
                                Console.ReadLine();
                                return;
                               
                            default:
                                Console.WriteLine("Re-enter operation");
                                oper = Convert.ToString(Console.ReadLine());                                                           
                                break;









                        } Console.ReadLine();


                } 
            } 
            
                }
           

              
        }
    }

This post has been edited by PsychoCoder: 12 May 2010 - 07:54 PM
Reason for edit:: Code tags added

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: using split within an array

Posted 12 May 2010 - 07:55 PM

I believe you have your Console.ReadLine() in the wrong place. Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_2_2
{
    class Program
    {
        static void Main()
        {
            double num1;
            double num2;
            double total;
            string[] equation = { num1 + num2, num1 - num2, num1 * num2, num1 / num2 };
            string inputString = Console.ReadLine;
            string input = inputString.Split(',');

            const string QUIT = "quit";
            Console.ReadLine();

            Console.WriteLine("Enter equation");
            equation[4] = Convert.ToDouble(Console.ReadLine());


            while (equation.Length != 0)
            {
                if (equation[4] = QUIT)
                    Console.WriteLine("GoodBye");


                while (oper != QUIT)
                {


                    switch (equation)
                    {
                        case equation[0]:
                            total = num1 + num2;
                            Console.WriteLine("{0} {1} {2} = {3}", equation[0], total);
                            num1 = total;
                            break;
                        case "-":
                            Console.WriteLine("Enter number");
                            num2 = Convert.ToDouble(Console.ReadLine());
                            total = num1 - num2;
                            Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                            num1 = total;
                            break;
                        case "*":
                            Console.WriteLine("Enter number");
                            num2 = Convert.ToDouble(Console.ReadLine());
                            total = num1 * num2;
                            Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                            num1 = total;
                            break;
                        case "/":
                            Console.WriteLine("Enter number");
                            num2 = Convert.ToDouble(Console.ReadLine());
                            total = num1 / num2;
                            Console.WriteLine("{0} {1} {2} = {3}", num1, oper, num2, total);
                            num1 = total;
                            break;
                        case "=":

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


                            break;
                        case "QUIT":
                            Console.WriteLine("Thank You for using my calculator!");
                            Console.ReadLine();
                            return;

                        default:
                            Console.WriteLine("Re-enter operation");
                            oper = Convert.ToString(Console.ReadLine());
                            break;









                    }


                }
            }
            Console.ReadLine();

        }



    }
}


Was This Post Helpful? 0
  • +
  • -

#6 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: using split within an array

Posted 12 May 2010 - 08:53 PM

I figured it out, I copied and pasted it into a new project and it has many errors. Something was wrong with how it loaded last time. Now I think I definitely have this code wrong. 10+ errors. It is going to be a long night. Thanks for your replies.

I can do most any of the other problems in my book but this calculator is ripping me a new one. Please help.
Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: using split within an array

Posted 12 May 2010 - 08:59 PM

Well what are the errors and we can help
Was This Post Helpful? 1
  • +
  • -

#8 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: using split within an array

Posted 12 May 2010 - 09:22 PM

Error 1 'System.Array' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Error 2 Cannot implicitly convert type 'string' to 'double'
Error 5 A value of an integral type expected
Error 6 A constant value is expected

I understand some what how to fix error 2 but I am having a hard time with it in this array.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: using split within an array

Posted 12 May 2010 - 09:26 PM

double num1;
double num2;
double total;
string[] equation = { num1 + num2, num1 - num2, num1 * num2, num1 / num2 };



Explain that block of code. What's the point behind your array? What is num1 + num2? Is it a string?
Was This Post Helpful? 0
  • +
  • -

#10 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: using split within an array

Posted 12 May 2010 - 09:33 PM

I am trying to initialize my array. Am I not suppose to string the numbers and operators?

I am trying to add to my code, an array, so that I can prompt for the equation and write it out in one line on the command prompt. I am just not grasping how.
Was This Post Helpful? 0
  • +
  • -

#11 bentodd  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: using split within an array

Posted 12 May 2010 - 10:15 PM

I know I am probably irritating the crap out of you guys with my ignorance but I appreciate any help you can give me. Do I have to use several arrays to build an equation or can I do it all in one like I have it or am I just doing other things wrong in my code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1