C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
CIN command, where?? Posted by capitolc on 19 Dec 2005 at 11:04 AM
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=-
Report
Re: CIN command, where?? Posted by weicco on 22 Dec 2005 at 3:16 AM
: 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:

int i = 33;
char c = (char)i;


Other way round it works implicitly: i = c;
Report
This post has been deleted. Posted by snakov on 23 Nov 2011 at 4:17 AM
This post has been deleted.
Report
Re: CIN command, where?? Posted by snakov on 23 Nov 2011 at 4:20 AM
You could use this C# cin class written by Svetlin Nakov. It can read numbers, ints, doubles, decimals and string tokens from the console, just like cin >> a >> b in C++.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.