C# - Reading Number Input (and other console commands)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

C# - Reading Number Input (and other console commands)

Post by RyanPridgeon »

Well I'm just starting learning C# properly, and I found some trouble finding out some things. So I decided to attempt to help people by posting this here.

- Also to the moderators; I think it would be better if you called this a tutorial forum, because in the end, most snippets end up teaching so much anyway!

So basically in C# you have your "using" (or includes/imports), then your namespace, then your program class, then your main function;

Code: Select all

using System;

namespace learncsharp
{
    class Program
    {
        static void Main(string[] args)
        {
           // program runs here
        }
    }
}
Here are some basic console commands;

Code: Select all

Console.Write("Hello, World!"); // writes hello world to the console

Console.WriteLine("Hello, World!"); // writes hello world to the console, and starts a new line

Console.Clear(); // clears the console screen

Code: Select all

string Thing;
Thing = Console.ReadLine(); // reads a line of keyboard input and stores it inside 'Thing'

And now for the part I struggled with;

Code: Select all

int Number; 
string Thing;
Thing = Console.ReadLine();
Number = int.Parse(Thing); // reads user input, then converts to a integer and stores inside 'Number'
Have fun

Code: Select all

// This is a program to help those who have trouble working out how to read
// numbers from the console (like I did)
// The Parse function looks very useful. Thankyou to functionx.com!

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

namespace learncsharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int Input; // a variable to hold the number
            string strNumber; // a variable to hold the user input

            strNumber = Console.ReadLine(); // take input from the keyboard
            Input = int.Parse(strNumber); // convert the input into a number

            Input += 5; // add five to the number
            Console.Write(Input); // write the number to the screen

            Console.ReadLine(); // wait for input to exit
        }
    }
}


- Ryan

EDIT:

You can also use this method for other data types

For example,

Code: Select all

Geoff = float.Parse(Jim);
Where Jim is a string that contains a float value, and Geoff is a float
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Keevu
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 16
Joined: Tue Mar 24, 2009 3:53 pm

Re: C# - Reading Number Input (and other console commands)

Post by Keevu »

Sweetness Im a newb to programming console, I prefer to program in windows form. But console is definetly useful thanks a ton.

C# for life! 8-)
Post Reply