CIN command, where?? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

CIN command, where??

capitolccapitolc Posts: 31Member
hey group

I am trying to practice and learn basic C# statements and syntax, and I fail to figure out how to get a user input from the keyboard. I've notice that

:Console.Write(); //console.WriteLine();

writes to the screen, but when I try to save an input to a variable, it seems to only work for interger numbers.

:Console.Read(); //console.ReadLine();

My problem is I am trying to read characters from the input and save them. I get an error about converting int to char. Is there any command so that I can save an input from the user and save as in a char variable.


:using System;
:class classname {
: public static void {
: char discipline;
: Console.Write("enter A or B");
: discipline = Console.Read(); // Cannot implicitly convert type
// 'int' to 'char'

:

-=The Best Has Yet To Come=-

Comments

  • weiccoweicco Posts: 950Member
    : hey group
    :
    : I am trying to practice and learn basic C# statements and syntax, and I fail to figure out how to get a user input from the keyboard. I've notice that
    :
    : :Console.Write(); //console.WriteLine();
    :
    : writes to the screen, but when I try to save an input to a variable, it seems to only work for interger numbers.
    :
    : :Console.Read(); //console.ReadLine();
    :
    : My problem is I am trying to read characters from the input and save them. I get an error about converting int to char. Is there any command so that I can save an input from the user and save as in a char variable.
    :
    :
    : :using System;
    : :class classname {
    : : public static void {
    : : char discipline;
    : : Console.Write("enter A or B");
    : : discipline = Console.Read(); // Cannot implicitly convert type
    : // 'int' to 'char'
    :
    : :
    :
    : -=The Best Has Yet To Come=-
    :

    You might want to read about types and type casting also :) C# doesn't cast from "bigger" types to smaller ones. Console.Read() returns Int32 which is 32 bit integer and char is 8 bit integer. This means that you'll have to explicitly cast it like this:

    [code]
    int i = 33;
    char c = (char)i;
    [/code]

    Other way round it works implicitly: i = c;
  • snakovsnakov Posts: 4Member
    This post has been deleted.
  • snakovsnakov Posts: 4Member
    You could use this [link=http://www.nakov.com/blog/2011/11/23/cin-class-for-csharp-read-from-console-nakov-io-cin/]C# cin class[/link] written by Svetlin Nakov. It can read numbers, ints, doubles, decimals and string tokens from the console, just like cin >> a >> b in C++.
Sign In or Register to comment.