0
votes

I am trying to create a random number generator in C++, which puts the result in a textBox.

I get the error 'error C2440: 'initializing' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'System::String ^

My code is:

int rnd = 1 + rand() % 100;
std::ostringstream convert;
convert << rnd;
String ^ num = convert.str();
textBox1->Text = num;

What am I doing wrong?

2
You are missing a language tag. String ^ num = ... is not C++. - juanchopanza
What do you mean? Sorry, I'm new to C++ (C#/VB.NET) - Joe
looks like microsoft managed c++ code - billz

2 Answers

1
votes

You cannot assign a std::string to a System::String. The first one is ISO c++ and the second one Microsoft C++/CLI.

As suggested here, you can do something like this

String^ num = gcnew System::String(convert.str().c_str());
-1
votes

The .Net way is

textBox1-> Text = rnd.ToString ();

ostringstream is a lousy way to do that conversion even in native C++.