Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# equivalent of cin?

Status
Not open for further replies.

Sidro

MIS
Sep 28, 2002
197
0
0
US
Hi,
Im using the console mode for C#. Im use to C++ and just started to migrate to C#. I was wondering if C# has cin>>X to read user input into a variable like C++? What would it be? thankx
 
Take a look at the StreamReader class.

You would create a new StreamReader that pointed to the console and set your encoding (defaults to UTF-8, but for console input you'll probably want ASCII). The Read method will then read one character.

This probably isn't 100% correct, but it should get you started:
Code:
StreamReader MySReader = new StreamReader(Console.OpenStandardInput, Encoding.ASCII);
StringBuilder MyString = new StringBuilder();
while (MySReader.BaseStream.Position <= MySReader.BaseStream.Length) {
  MyString.Append(MySReader.Read());
}
MySReader.Close();
MySReader.Dispose();
I'm not sure that the call to Dispose is required, as you normally need that when opening a file handle. But since the console input is a virtual file handle, maybe you do, maybe you don't need to call it.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
the Console class supports 2 static methods for reading: Console.ReadLine() and Console.Read(), both of which return strings.

You can then use Int.Parse(string) (or Float.Parse() or whichever value type you need) to get the data from the returned string. An added nicety is that the Parse() functions throw exceptions when they encounter a funky string which they are unable to parse.
 
Now that you see from the postings above that C# requires direct method calls instead of cin >> x style programming, you may wonder why nobody has ported this C++ chestnut to C#. One part of the answer is that differences between overloading in C++ and C# would make porting that particular C++ idiom difficult.

For instance, in C++ the declaration gets to designate which arguments are &quot;by ref&quot;, whereas in C# both the caller and the callee must agree on &quot;by ref&quot;. This makes it difficult to do
cin >> x
because in C# this type of declaration
-> public static istream operator>>(istream i, ref int x)
would not be legal/logical -- and is not allowed in C#. If it were allowed, the use of it would have to appear in C# as something like this
cin >> (ref x);
Even if it were legal, it stops mattering at that point, because nobody would want to use it.

You are entering a new world on your switch from C++/Templates/STL to C#. You will probably feel a little more at home when generics (like templates with more type safety) arrive, but even so, only the concepts and broader outlines of C++ syntax (and not as much of the API specifics) will come with you on your move from C++ to C#.

By the way, you may be interested to take a look at managed C++. If you create a C++ project in VS7 and set &quot;Use Managed Extensions&quot; to &quot;yes&quot; (under configuration properties->General), you will be able to make CLR assemblies in C++ *and* use iostream.
 
> you will be able to make CLR assemblies in C++ *and* use iostream. <

I don't think you can do this in the same class. For a managed class, you have to preface it with __gc which means that everything in there is managed. If you want to do &quot;old-skool&quot; C++, then you'll need to put it in another class, and then to call it from the managed world, write a wrapper class.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top