Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 6th, 2003, 07:43 AM
Registered User
 
Join Date: Aug 2003
Location: tehraan, , Iran.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert char to int does not work

will you please tell me why the conversion does not work ,in this code!! (I'm abegginer!!!!):)

using System;

namespace ConsoleApplication4
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            string strPoly;
            int[] Int = new int [10];
            int[] num = new int [3];
            int c=0; //flag for checking number of integers;
            int temp=0;
            int i=0,exp =1;
            int j=0;
            int flag=0;
            strPoly = Console.ReadLine();
            foreach ( char obj in strPoly)
            {
                switch (obj)
                {
                    case '+' :
                    case '=' :
                    case '-' :
                    case '^' :
                    case '*' :
                        flag =1;
                        goto conversion;

                    default:
                        if ( 47 < obj & obj < 58)
                        {
                            Console.WriteLine("temp:{0}",temp);
                            Int[i] = Convert.ToInt32(obj);///THIS IS THE LINE
                            Console.WriteLine("temp1={0}",temp);
                            i++;
                        }
                        else
                            Console.WriteLine("there is an invalid or Unknown character '{0}' in the polynomial you enter!",obj);
                        break;
                }
            conversion :

                if (flag == 1)
                {
                    exp=1;
                    j=0;
                    for( temp =i-1; temp >= 0;temp--)
                    {
                        num[c] =Convert.ToChar(Int[temp])*exp + num[c];
                        exp =exp * 10;
                    }
                    Console.WriteLine("{0}th number is {1}:",c,Convert.ToInt32(num[c]));
                    c++;
                    i=0;
                    flag = 0;
                }
            }

            Console.ReadLine();

        }
    }
}






  #2 (permalink)  
Old August 6th, 2003, 10:39 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to comedyox
Default

> if ( 47 < obj & obj < 58)

You probably don't mean to logically AND those two expressions. You probably want something like this:

      if ((47 < obj) && (obj < 58))

> Int[i] = Convert.ToInt32(obj);///THIS IS THE LINE

...and what is the error message you get? Does it happen at compilation time or run time? What exactly happens?

Having said all that, it looks like you'd be better off using a regular expression anyway.

Try this one on for size:

// sample begins

using System;
using System.Text.RegularExpressions;

namespace ParsePoly
{
  class ParsePoly
  {
    public static void Main()
    {
      string poly = "";
      while(poly == "")
      {
        Console.Write("Enter your polynomial: ");
        poly = Console.ReadLine();
      }
      Regex rx = new Regex(@"[^\^\+\-\*\=0-9]");
      MatchCollection m = rx.Matches(poly);
      if (m.Count > 0)
        Console.WriteLine("ParsePoly: illegal polynomial");
      else
      {
        string [] numbers = poly.Split('^','*','+','-','=');
        int i = 0;
        foreach(string s in numbers)
        {
          Console.WriteLine("{0}th number is {1}", ++i, s);
        }
      }
    }
  }
}

// sample ends - example output follows

C:>parsepoly
Enter your polynomial: 123+234-345*456^567=678
1th number is 123
2th number is 234
3th number is 345
4th number is 456
5th number is 567
6th number is 678

C:>parsepoly
Enter your polynomial: 123f
ParsePoly: illegal polynomial

HTH,
()x
Also a beginner.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
Convert a char to int!!!!?? vtvb C++ Programming 6 March 18th, 2006 05:51 AM
How to Convert a int in a bool with FormatStrings ShadowMoon Classic ASP Basics 2 October 26th, 2004 05:53 AM
Convert int to nvarchar bekim SQL Language 1 August 12th, 2004 06:33 AM
Want to reiterate on my char to int problem CNewbie C++ Programming 0 April 2nd, 2004 12:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.