12 Replies - 4919 Views - Last Post: 08 February 2009 - 12:40 PM Rate Topic: -----

#1 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

System::String^ to std::String

Posted 01 February 2009 - 03:06 PM

I've looked all over the internet but I cannot find a method that works correctly. I always end up getting errors on compiling.

So far I've tried about 3 different methods.

one of the methods I tried was a straight conversion which didn't work, and I didn't expect it to really:
string input;
input = txtInput->Text; // Text is of type System::String^


Can anybody help?

Is This A Good Question/Topic? 0
  • +

Replies To: System::String^ to std::String

#2 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: System::String^ to std::String

Posted 01 February 2009 - 03:47 PM

Are you trying to get user input stored to string? I'm a little confused as to your objective here.

Try using something like:

string input;

cout >> "Enter input here..."; // will ask for user input
cin >> input; // stores user input into input string
cout << input; //displays input string



*Edit* Misread your topic, sorry.

This post has been edited by kiasta: 01 February 2009 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: System::String^ to std::String

Posted 01 February 2009 - 08:51 PM

View Postkiasta, on 1 Feb, 2009 - 02:47 PM, said:

Are you trying to get user input stored to string? I'm a little confused as to your objective here.

Try using something like:

string input;

cout >> "Enter input here..."; // will ask for user input
cin >> input; // stores user input into input string
cout << input; //displays input string



*Edit* Misread your topic, sorry.

No, from the looks of it, he is using VC++ GUI, therefore, cin, and cout is completly useless. It looks like he is trying to receive the text from and EditBox named txtinput and use it for something that cannot accept system::string. This happens quite often in VC GUI

A little advice, get rid of it. In order to do this is to learn some MFC. I got borland C++ builder 6 instead. has the same features, if not more, with less confusion.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: System::String^ to std::String

Posted 01 February 2009 - 08:59 PM

Is there particular reason you want a plain string instead of the managed one? The only way to really do this is to Marshal it (if you are using VC++ 2008) or to use some funky tricks. And if it is not absolutely necessary, I would just use the managed string for what you need to do. It is safer to use the managed string as well.

:)
Was This Post Helpful? 0
  • +
  • -

#5 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: System::String^ to std::String

Posted 02 February 2009 - 12:05 AM

View PostMartyr2, on 1 Feb, 2009 - 07:59 PM, said:

Is there particular reason you want a plain string instead of the managed one? The only way to really do this is to Marshal it (if you are using VC++ 2008) or to use some funky tricks. And if it is not absolutely necessary, I would just use the managed string for what you need to do. It is safer to use the managed string as well.

:)

I need it because I have already written a program using std::string, and all I am doing now is using VC++2008 to create its GUI. I've managed to convert a std::string to System::String^ but I can't seem to do it the other way around. Of course this "bad" and "lazy" implementation is only temporary and later on I will adapt the whole of my program to work with System::String^ but for now I just want to play around with the GUI.
Was This Post Helpful? 0
  • +
  • -

#6 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: System::String^ to std::String

Posted 02 February 2009 - 09:13 AM

if you want to input a line from the user,

cin >> A .. will only take 1 word and white space will be terminator !
these operator will work on IOS, FileStream, strings, and systemString

you have 2 options, either to set noSkipWhiteSpaces Flag
.. or to use this,

	string text;
	cout << "enter your full name: ";
	cin.getline( text );


maybe using = operator would work !

but you are trying to convert System::string^ to Std::string ...
i think System::string might be a complex template type (class) ...

so i suggest you try this, as long as Class String will expect a char* or a String,
char* is a POINTER .. so the magic will start now,

when pointer are used to make Array thus read through it,
will a constant pointer works in the same way .. Yes it does,

"if we can catch the Snake's head .. we can stream the fire to its tail" thus,
if we can only point char* pointer using dynamic_cast to the 1st element of the system::string^
we can take control of it ...
____________________________

Next, if System::String is using <char> or <char traits<t>>,
then conversions would work at the first place

but if it's a UNICODE using wchar_t, then there comes the coding problem
because Std::String and Char[] is char on ASCII ...

This post has been edited by Plus: 02 February 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: System::String^ to std::String

Posted 02 February 2009 - 09:42 AM

A Google search of System.String to std::string yields lots, including http://msdn.microsof...623(VS.80).aspx.
Was This Post Helpful? 0
  • +
  • -

#8 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: System::String^ to std::String

Posted 02 February 2009 - 01:35 PM

View PostJackOfAllTrades, on 2 Feb, 2009 - 08:42 AM, said:

A Google search of System.String to std::string yields lots, including http://msdn.microsof...623(VS.80).aspx.

I got it to compile finally.
the function calc takes a std::string as an argument and returns a std::string. According to the example you gave me this should work... but I get a debug message: Expression: string subscript out of range (in file xstring)
private: System::Void btnEquals_Click(System::Object^  sender, System::EventArgs^  e) {
				 string input, output;
				 MarshalString(txtInput->Text, input);
				 output = calc(input);
				 System::String^ answer = gcnew System::String(output.c_str());
				 txtCompAns->Text = answer;
			 }

Was This Post Helpful? 0
  • +
  • -

#9 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: System::String^ to std::String

Posted 08 February 2009 - 03:51 AM

I no longer need any more conversion help since I have converted the whole of calc() to use System::String^, which is much better to have it CLR compliant. And it looks alot more neat and proffessional now. I have also come to prefer System::String^ than std::string as it is a much more robust class with easy conversions to other types such as double without the need for streams.

If a moderator would close this topic I would appreciate it.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: System::String^ to std::String

Posted 08 February 2009 - 11:24 AM

We do not close topics at dream in code unless there is a good reason for it. We try to keep them open in case another user may wish to add some salient information...not necessarily for your benefit, but for the benefit of others who may be searching for the same issue.
Was This Post Helpful? 0
  • +
  • -

#11 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4188
  • View blog
  • Posts: 11,857
  • Joined: 18-April 07

Re: System::String^ to std::String

Posted 08 February 2009 - 11:34 AM

You should have been here last week Amadeus, we had one guy that was insisting we delete his post and was even trying to deface the thread in order to get us to do it. Apparently he was cheating and didn't want anyone to know and when we locked the thread to prevent any other changes he was pretty upset. Paid us a visit on the chat room even and wasn't happy.

Where were you then? WHERE WERE YOU! hehe jk. :)
Was This Post Helpful? 0
  • +
  • -

#12 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: System::String^ to std::String

Posted 08 February 2009 - 11:59 AM

LOL, I always seem to miss the good times :)
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,137
  • Joined: 14-September 07

Re: System::String^ to std::String

Posted 08 February 2009 - 12:40 PM

How is asking for help online cheating?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1