simple C# question!

Topics on common programming languages
Post Reply
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

simple C# question!

Post by Trebor29 » Tue Sep 21, 2010 9:58 pm

Hi,
Ive just started learning C# with VFisual Basic 2010 and this is only the second program lol..! I know is very similar to JAVA but im not doing something right...!
Can anyone tell me why this is not reading 'if (num <= 9)'.... for some reason when I Debug, if I input a 9, num becomes 57 and so obviously jumps to the 'else if'.

Code: Select all

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

namespace NumberRange
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please input a number.");
            int num = Console.Read();
            if (num <= 9)
            {
                Console.WriteLine("This number is within the specified range.");
            }
            else if (num > 9)
            {
                Console.WriteLine("Sorry, this number is out of range.");
                Console.WriteLine("Please try another.");
            }
        }
    }
}
Thank you... :roll:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: simple C# question!

Post by Neo » Wed Sep 22, 2010 12:37 pm

Use int num = int.Parse(Console.ReadLine()); instead of int num = Console.Read();.
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: simple C# question!

Post by Trebor29 » Wed Sep 22, 2010 9:14 pm

Hi Neo,

Thanks 'again' for your help... It is only you who replies to my questions.. lol
So what is '.Parse'?? and can you tell me why it was calculating those crazy numbers before...?

Cheers. Trebor :ugeek:
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: simple C# question!

Post by Neo » Wed Sep 22, 2010 9:22 pm

It was reading console input as a string/char and you need to get is parsed as an integer (number value).
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: simple C# question!

Post by Trebor29 » Wed Sep 22, 2010 10:47 pm

erm... just out of curiosity Neo, Why was it reading Console input as a string when I declared the variable 'num' as an int? Is this just one of the differences from using JAVA?
They will probably go over this next week but I like to get ahead of the class. ;)
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: simple C# question!

Post by Neo » Thu Sep 23, 2010 4:28 am

You will need to understand a programming point here.
When you define it as int num = myfunction(); there is no effect to myfunction() function from "int" since you do not pass it as a parameter to it. Within the function, there is no way to identify that you are going to assign the function return to an "int".

If you consider the definition of the function, you will notice that it returns a string. You can't assign a string to an "int" directly without converting it to a number.
User avatar
Trebor29
Lieutenant
Lieutenant
Posts: 75
Joined: Thu Apr 29, 2010 12:34 am

Re: simple C# question!

Post by Trebor29 » Thu Sep 23, 2010 9:54 pm

Hummm.... interesting! I still have much to learn "said the young apprentice" :)
Thanks Neo.
Post Reply

Return to “.Net & Other Programming”