| View previous topic :: View next topic |
| Author |
Message |
OSIRIS Grandmaster Cheater
Reputation: 0
Joined: 27 Aug 2006 Posts: 654
|
Posted: Sat Oct 25, 2008 9:47 am Post subject: C# int conversion. |
|
|
| Code: |
Console.WriteLine(
" *****************************************************"
);
Console.WriteLine(
" * [Stefon]'s Addition *"
);
Console.WriteLine(
" * Calculator *"
);
Console.WriteLine(
" *****************************************************"
);
Console.WriteLine("");
Console.WriteLine("Please enter two numbers that will be added together");
Console.WriteLine(" ");
Console.Write("Number 1: ");
string num1 = Console.ReadLine();
Console.Write("Number 2: ");
string num2 = Console.ReadLine();
string sum = num1 + num2;
Console.WriteLine(sum);
Console.ReadLine(); |
If you ran this code and the two numbers you entered were "4" and "5" the output would be "45". The output I want is "9". I know what I got to do. I got to get num1 and num2 and sum to be a int. But the contents of a Console.ReadLine(); cannot be an int. So I have it as as string right now.
So how do I convert my strings into an int.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Oct 25, 2008 9:50 am Post subject: |
|
|
int nNum1 = num1.ToInt32();
??
_________________
|
|
| Back to top |
|
 |
OSIRIS Grandmaster Cheater
Reputation: 0
Joined: 27 Aug 2006 Posts: 654
|
Posted: Sat Oct 25, 2008 9:58 am Post subject: |
|
|
| lurc wrote: | int nNum1 = num1.ToInt32();
?? |
Nope.
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Sat Oct 25, 2008 11:03 am Post subject: |
|
|
| Code: | int sum = Convert.ToInt32(num1) + Convert.ToInt32(num2);
Console.WriteLine(Convert.ToString(sum)); |
|
|
| Back to top |
|
 |
--Pillboi-- Grandmaster Cheater Supreme
Reputation: 0
Joined: 06 Mar 2007 Posts: 1383 Location: I don't understand the question. Is this a 1 to 10 thing?
|
Posted: Sat Oct 25, 2008 11:42 am Post subject: |
|
|
A simple google search would have sufficed. ToDecimal(String) or Int32.Parse(input); or ToUInt32(String)
_________________
Enter darkness, leave the light, Here be nightmare, here be fright...
Earth and Water, Fire and Air. Prepare to meet a creature rare.
Enter now if you dare, Enter now the dragon's lair. |
|
| Back to top |
|
 |
OSIRIS Grandmaster Cheater
Reputation: 0
Joined: 27 Aug 2006 Posts: 654
|
Posted: Sat Oct 25, 2008 11:48 am Post subject: |
|
|
| HolyBlah wrote: | | Code: | int sum = Convert.ToInt32(num1) + Convert.ToInt32(num2);
Console.WriteLine(Convert.ToString(sum)); |
|
Thanks it worked.
+REP
|
|
| Back to top |
|
 |
blackmorpheus Expert Cheater
Reputation: 0
Joined: 05 Apr 2008 Posts: 159
|
Posted: Sat Oct 25, 2008 12:00 pm Post subject: |
|
|
| Code: | | string num1 = Console.ReadLine(); |
Why do you read is as string ???
Why not integer??
|
|
| Back to top |
|
 |
OSIRIS Grandmaster Cheater
Reputation: 0
Joined: 27 Aug 2006 Posts: 654
|
Posted: Sat Oct 25, 2008 12:04 pm Post subject: |
|
|
| blackmorpheus wrote: | | Code: | | string num1 = Console.ReadLine(); |
Why do you read is as string ???
Why not integer?? |
It wouldnt let me do
int num1 = Console.ReadLine();
|
|
| Back to top |
|
 |
Stylo Grandmaster Cheater Supreme
Reputation: 3
Joined: 16 May 2007 Posts: 1073 Location: Israel
|
Posted: Sat Oct 25, 2008 12:06 pm Post subject: |
|
|
but it will let you do it like this
| Code: |
int num1 = int.Parse(Console.ReadLine());
|
_________________
Stylo |
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Sat Oct 25, 2008 5:04 pm Post subject: |
|
|
Uhhh...
| Code: | | int1 = Console.Read(); |
Was it that hard?
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Oct 25, 2008 5:34 pm Post subject: |
|
|
| Frostbyt3 wrote: | Uhhh...
| Code: | | int1 = Console.Read(); |
Was it that hard? |
Not quite.
Console.Read() returns one char, and it'll return the ASCII value, not the actual integer that the symbol represents.
_________________
|
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Sat Oct 25, 2008 5:51 pm Post subject: |
|
|
Really? Mine's returning the numerical value..
If i type in 1337, it doesn't come out as 49(ASCII for 1), it comes out 1337.
Weird.
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sat Oct 25, 2008 7:45 pm Post subject: |
|
|
int.parse(<string here>);
thats the way i mainly use
either that or
Convert.ToInt32(<string>);
_________________
|
|
| Back to top |
|
 |
OSIRIS Grandmaster Cheater
Reputation: 0
Joined: 27 Aug 2006 Posts: 654
|
Posted: Sat Oct 25, 2008 7:47 pm Post subject: |
|
|
| yoyonerd wrote: | int.parse(<string here>);
thats the way i mainly use
either that or
Convert.ToInt32(<string>); |
I liked Convert.ToInt32(<string>);
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sun Oct 26, 2008 12:06 am Post subject: |
|
|
both work, so, whatever you're more comfotable with really
_________________
|
|
| Back to top |
|
 |
|