MrZkitten

now i remember why i abandoned 'c' for basic and pascal, data handling.

what i want to do is fairly simple, but when appled to standard data is fairly usual in a business program.

i want to input a 'number' from the console and manipulate it.. thus...

in basic:

input "please enter a number:";a

input "please enter a number:";b

print "the sum of ";a;" plus ";b;" is "; a + b

i have this so far in c# (console app)

static void Main(string[] args)

{

string userName,numberA, numberB;

Single a, b;

Console.WriteLine("hello ");

Console.Write("what be your name:");

userName = Console.ReadLine();

Console.Write(userName );

Console.Write(" please enter a number:");

a =Console.ReadLine(); // this does not work, i intend to supliment it some how with

// a function to input numberA as string then convert to single 'a'

// but i have yet to find a complete reference to c#

Console.Write ("please enter another number:");

b = Console.ReadLine(); //same as for 'a' unless there is a simple way to read in a number

Console.writeline("their sum is {0}", a + b);

Console.Write(" press enter to exit");

Console.Read();

}

help.. is there a definative reference, the online stuff and the how do i is not much help,

i have what i thought was an instruction manual, ";visual c# from novice to professional" and it simply does not help. any one have a suggestion for a reference. I am a writer of sorts and i assure you i am working on a if you did this in 'basic' you should try this in 'c#' wicki.

/meow/

my new web site is (some now done in visual studio) at:

mrzkitten.org




Re: Visual C# General string to a number... plus can you simply read a number from standard i/o

Matthew Watson

There's nothing built-in, but it's easy to write a set of input functions. In the sample below, note that "float" is the same as "Single":



class Program
{
static void Main(string[] args)
{
float number = InputFloat("Please enter a number: ");
Console.WriteLine("You entered the number: " + number);
}

static float InputFloat(string prompt)
{
for (;;)
{
Console.Write(prompt);
string input = Console.ReadLine();

float result;

if (float.TryParse(input, out result))
return result;

Console.WriteLine("That is not a number.");
}
}
}







Re: Visual C# General string to a number... plus can you simply read a number from standard i/o

MrZkitten

things i discovered...

one can not just read a number from the standard input, but it is simple to get the number:

in basica:

input "please enter a number ";x

this is what i finally ended up with (close to above)

static float getnumber(string prompt)

{

float x;

string numberX;

int y;

y = -1;

x = 0;

while (y == -1) // make sure a valid number is entered

{

try

{

Console.Write(" please enter {0} number:",prompt);

numberX = Console.ReadLine();

x = float.Parse(numberX);

y = 0;

}

catch (Exception)

{

Console.WriteLine("please enter a valid #");

}

}

return x;

}

the unique (to a new programer) is the try instruction. Here we are assuming that it is the error would be a bad or missing number.

the instruction :

type.parse(string)

is what scans the string and returns the number. kind of not explained in any text. but something to add to ones note book if your learning c# . I am working on a page at mrzkitten.org that will detail my efforts. c# is an interesting marriage of basic and c++ , i can see the underlying power that it will give me as i work to my ultimate goals.

thanks for all the help.