FlyBoy Posted October 1, 2004 Posted October 1, 2004 im trying to do the following thing (using C#): static void main(string [] args) { string test = Console.Readline(); int num=System.Int32.Prase(test[0]); } /// The aforementioned example doesnt work....so i've tried another way /// as following: static void main(string[] args) { string test = Console.Readline(); int num=(int)test[0]; /// Sort of casting. } // also doesnt work....any idea??? Quote
Administrators PlausiblyDamp Posted October 1, 2004 Administrators Posted October 1, 2004 try int num = int.Parse(test[0]); Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
FlyBoy Posted October 1, 2004 Author Posted October 1, 2004 try int num = int.Parse(test[0]); oh..i see. 10x!!! Quote
FlyBoy Posted October 1, 2004 Author Posted October 1, 2004 try int num = int.Parse(test[0]); ok,i've tested it now...(just came home) and it doesnt work..this is the error that im getting: Argument '1': cannot convert from 'char' to 'string'any any idea? Quote
Administrators PlausiblyDamp Posted October 1, 2004 Administrators Posted October 1, 2004 int num=System.Int32.Parse(test[0].ToString() Not sure if that will acheive what you are after though - it may be better if you explain what you are trying to do exactly. Are you trying to get the numerical value of the character or obtain it's ASCII value? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
FlyBoy Posted October 1, 2004 Author Posted October 1, 2004 int num=System.Int32.Parse(test[0].ToString() Not sure if that will acheive what you are after though - it may be better if you explain what you are trying to do exactly. Are you trying to get the numerical value of the character or obtain it's ASCII value? ok ,what im trying to do is : i want to take an input(numerical) from a string for example : string str=Console.ReadLine(); and them summerize all the numbers in it....lets say : str="5678"; the output will be : 5+6+7+8; = 18. so the output will be 18. thats easy as walking in the park..but how to convery each number in the string to a value int type...????? tried using int...but int type doesnt support indexing... :( :o 10x in advance. Quote
Leaders snarfblam Posted October 2, 2004 Leaders Posted October 2, 2004 You want to add up the digits??? The problem appears to be that you are trying to parse a char. I believe you can only parse a string. What you might want to do (if you are trying to do what i think you are) is take the unicode values... subtract 48 (this is the unicode for 0; it will yeild 0 for '0' and 1 for '1' and 2 for '2,' etc...) and add those up. You can also convert each char to a string and parse those. I don't really know c#, but i know c++ and i know vb so lets see what happens... int addsomedigits(void) { string str=Console.ReadLine(); int len = str.Length; int total = 0; for (int i = 0; i < len; i++) total += System.Int32.Parse(str[i].ToString); } [/Code] My casing is probably not 100% correct... I usually program in VB. But the main idea is to create a "total" variable to store a running total and use a for loop to loop through the chars, convert them to strings, parse them as int32s, and add them to total. I don't have C# so I can't test this... but i tried. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.