Monday, January 23, 2012

Get user input from Console, ReadLine()

The function Console::ReadLine() reads a complete line from user inpt from console, terminated when ENTER key pressed.

Example:
Example of using Console::ReadLine()
// CLRConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{

 Console::WriteLine(L"type in something, then press ENTER:");
 String^ userInput = Console::ReadLine();

 Console::WriteLine("Thanks! you enter: " + userInput);

 Console::ReadLine();
 return 0;
}


Please note that the data returned from Console::ReadLine() is in type of String^. It's a handle that reference a String object. To know more about "^", please refer the post ^ (Handle to Object on Managed Heap).

No comments:

Post a Comment