Newbie: Convert from string to integer

P

Paul Hemans

Hi I am trying to take input from the console and put it into an integer.
The code below is my attempt, it works, but is there a way that .Parse can
be used without referring to an existing variable (whose value would be
irrelevant anyway).
#include "stdafx.h"

using namespace System;

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

{

int width=0;

String^ line;

Console::WriteLine(L"Enter room width\n");

line = Console::ReadLine();

width = width.Parse(line);

Console::WriteLine(L"Pressed \n{0}", width);

return 0;

}

Something like

width = int.Parse(line);

would be nice.
 
P

pvdg42

Paul Hemans said:
Hi I am trying to take input from the console and put it into an integer.
The code below is my attempt, it works, but is there a way that .Parse can
be used without referring to an existing variable (whose value would be
irrelevant anyway).
#include "stdafx.h"

using namespace System;

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

{

int width=0;

String^ line;

Console::WriteLine(L"Enter room width\n");

line = Console::ReadLine();

width = width.Parse(line);

Console::WriteLine(L"Pressed \n{0}", width);

return 0;

}

Something like

width = int.Parse(line);

would be nice.
Or, how about this?

int a = 0;

Console::WriteLine(L"Enter an integer");

String^ b = Console::ReadLine();

a = Convert::ToInt32(b);

Console::WriteLine(L"You entered {0}!",a);
 
P

Paul Hemans

Yes that's it
I had tried:
Int32.Parse(..)
but not
Int32::parse(..)
Am I right in saying that the first version would be looking for an object
called Int32 and attempting to access a method called Parse() whereas the
second version is calling a static method of the class Int32?
 
P

Paul Hemans

Yes works well thanks.

pvdg42 said:
Or, how about this?

int a = 0;

Console::WriteLine(L"Enter an integer");

String^ b = Console::ReadLine();

a = Convert::ToInt32(b);

Console::WriteLine(L"You entered {0}!",a);
 
T

Tamas Demjen

Paul said:
Yes that's it
I had tried:
Int32.Parse(..)
but not
Int32::parse(..)
Am I right in saying that the first version would be looking for an object
called Int32 and attempting to access a method called Parse() whereas the
second version is calling a static method of the class Int32?

Exactly. Except there's no such thing as Int32.Parse, there is only
integer_variable.Parse, or Int32::parse.

In C++, the "instance.member" or "instance->member" syntax denotes a
non-static member. In such cases, "member" is unique for each instance.
For example, value.ToString() converts a particular object to string.
"instance" must be a variable, not a class. "instance->member" is just a
shortcut for "(*instance).member".

In contrast, the "Class::member" syntax indicates a static member, which
means all instances of the same class share the same member. For
example, Int32::parse(a_string) indicates that you want to use an
integer parser in general, but you have no particular integer value to
work with.

Also, "::" is used to specify a particular namespace, such as
"System::Int32", which tells the "Int32" identifier is within the
"System" namespace.

Tom
 
P

Paul Hemans

Thanks for taking the time to explain Tom. I think I understand what you are
getting at.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top