ToString behavior

J

jason.a.malone

Can somebody please tell me why:

#include <iostream>

int main(int argc, char *argv[]) {
int ia;
ia = 23432234;
std::cout << ia.ToString() << '\n';
}

always produces an output of 1? Actually, I should rephrase that...

When compiling Visual C++ .NET 2003 warns that "warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)" So, I understand that is why it always
produces a 1.

What I dont understand is why it has to be a bool. I thought the point
of ToString was to convert the int to a char*. What am I missing? If
this is not the correct way to do this can someone point me in the
right direction?

Please be gentle as I am VERY new to c++.

Thanks,
Jason
 
A

Artie Gold

Can somebody please tell me why:

#include <iostream>

int main(int argc, char *argv[]) {
int ia;
ia = 23432234;
std::cout << ia.ToString() << '\n';

I don't know what this is, but it's not C++ (and it wouldn't compile in
C++ as `ia', being an int, is not an aggregate, hence there's nothing to
`dot' from.

If it's a .net thing, an MS newsgroup would be an appropriate place to ask.
}

always produces an output of 1? Actually, I should rephrase that...

When compiling Visual C++ .NET 2003 warns that "warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)" So, I understand that is why it always
produces a 1.

What I dont understand is why it has to be a bool. I thought the point
of ToString was to convert the int to a char*. What am I missing? If
this is not the correct way to do this can someone point me in the
right direction?

Please be gentle as I am VERY new to c++.
I was. ;-)

HTH,
--ag
 
I

Ioannis Vranos

Can somebody please tell me why:

#include <iostream>

int main(int argc, char *argv[]) {
int ia;
ia = 23432234;
std::cout << ia.ToString() << '\n';
}

always produces an output of 1? Actually, I should rephrase that...

When compiling Visual C++ .NET 2003 warns that "warning C4800:
'System::String __gc *' : forcing value to bool 'true' or 'false'
(performance warning)" So, I understand that is why it always
produces a 1.

What I dont understand is why it has to be a bool. I thought the point
of ToString was to convert the int to a char*. What am I missing? If
this is not the correct way to do this can someone point me in the
right direction?

Please be gentle as I am VERY new to c++.


Although I also program in .NET I am about to get a headache too. :)


At first ia.ToString() is a .NET specific feature and is not part of ISO
C++.


What you should do is learn some ISO C++ first and then move on to .NET


Check a page of mine:

http://www23.brinkster.com/noicys/learningcpp.htm


Now in your case, int maps to the .NET type Int32.


Int32::ToString() returns a String * pointer.


cout has not any operator overload for a String *. And that's why it
outputs non-sense.


But you have got yourself in quite a mess. Learn first some ISO C++,
that is the language itself, and then .NET.


Your subject is off topic here, since the topic of this group is ISO C++.


You should ask in microsoft.public.dotnet.languages.vc newsgroup.


If it doesn't appear in your news server use the public MS news server:

msnews.microsoft.com



To be technically accurate, your code corrected:


#using <mscorlib.dll>

int main()
{
using namespace System;

int ia;
ia = 23432234;

// Or Console::WriteLine(ia.ToString());
// but prefer the one below:
Console::WriteLine("{0}", ia.ToString());
}
 

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

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top