Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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();

        }
    }
}






Reply With Quote
  #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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:41 PM.


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