How can I make Console::ReadLine accepts int? In other words.

  • Thread starter Thread starter Allen Maki
  • Start date Start date
A

Allen Maki

Hi everybody,

I need your help.

I am using Visual C++ .NET

How can I make Console::ReadLine accepts int? In other words.

I know I can do this:

string* i = Console::ReadLine();

Can anybody tell me how can I do this:

int i = Console::Readline();
 
Can you do this.

int i = (int)Console::Readline();

or

string* i = Console::ReadLine();
int x;
x = (int)i;

Lit
 
Lit,

You can't cast from string to int ;-) You have to convert it using
Convert::ToInt32(),
Int32::Parse(), Int32::TryParse() static methods.

System::String^ str = Console::ReadLine();

int x = Convert::ToInt32(str);
// or
int y = Int32::Parse(str);
// or

int z;
if (Int32::TryParse(str, z))
{
// ok you can use it
}
else
{
Console::WriteLine(
String::Format(L"'{0}' is not a valid number", str));
}

hope this helps
 
Back
Top