Need simple conversion from C++ to C#
-
Saturday, February 21, 2009 11:58 PMNew to C#. Could someone interpret the following C++ code into C# so that I can do some console application debugging. I can't quite figure out how the Console.Read() and Console.ReadLine() functions could do this:
#include <iostream>
using namespace std;
int main
{
int input;
cout << "Enter value:";
cin >> input;
return 0;
}
I've tried the following code but it just prints out the askey value and then halts.
int test;
Console.Write("Enter value:");
test = Console.Read();
Console.Write(test);
Console.ReadLine();
All Replies
-
Sunday, February 22, 2009 2:59 AM
using System.IO;
[STAThread]
static int Main ( string[] args )
{
Console.Write ( "Enter Name" );
string text = Console.ReadLine ( );
return 0;
}
I don't quite understand what int input is doing up there.
AlexB- Marked As Answer by Guo Surfer Thursday, February 26, 2009 8:04 AM
-
Sunday, February 22, 2009 9:30 AM
Using System;
[STAThread]
static int Main(string[] args)
{
int test;
Console.WriteLine("Enter value:");
string value = Console.ReadLine();
int.TryParse(value, out test);
/// Do some process with test.
return 0;
}
Thanks, A.m.a.L- Proposed As Answer by JohnGrove Sunday, February 22, 2009 4:10 PM
- Marked As Answer by Guo Surfer Thursday, February 26, 2009 8:04 AM
-
Sunday, February 22, 2009 3:32 PM
There is no direct .NET equivalent to "cin". You are free to use the 'SimulateCin' helper class that we use in our conversions:
using System; int test() { int input; Console.Write("Enter value:"); input = SimulateCin.ReadToWhiteSpace(true); return 0; } //---------------------------------------------------------------------------------------- // Copyright © 2006 - 2009 Tangible Software Solutions Inc. // // This class provides the ability to convert basic C++ 'cin' behavior. //---------------------------------------------------------------------------------------- internal static class SimulateCin { private static bool goodlastread = false; internal static bool LastReadWasGood { get { return goodlastread; } } internal static string ReadToWhiteSpace(bool skipleadingwhitespace) { string input = ""; char nextchar; if (skipleadingwhitespace) { while (char.IsWhiteSpace(nextchar = (char)Console.Read())) { } input += nextchar; } while ( ! char.IsWhiteSpace(nextchar = (char)Console.Read())) { input += nextchar; } goodlastread = input.Length > 0; return input; } }
Convert between VB, C#, C++, Java, & Python (http://www.tangiblesoftwaresolutions.com)- Marked As Answer by Guo Surfer Thursday, February 26, 2009 8:04 AM

