Reputation: 499
This is a Windows Form program written in c++. The objective of this, is getting a word, written by the user in the TextBox called tbInputSrc, which is used to search in a code in a file. The file I opened in this program, contains this:
1111 aaaa aaaa 1
2222 bbbb bbbb 3
3333 cccc cccc 5
4444 dddd dddd 7
5555 eeee eeee 7
The numbers are the code (codice), the first "word" is the name (nome), the second "word" is the surname (cognome), and the number is the mark (vote) of the student. So the objective is, to show, in a TextBox, name and surname of the student trough the code written in the TextBox.
ifstream input("output.txt");
string cognome, nome;
string text;
int codice, voto;
int tr;
tr = 0;
while (!tr && input >> codice >> cognome >> nome >> voto) {
if (this->tbInputSrc->Text == Convert::ToString(codice)) {
tr = 1;
}
}
if (!tr) {
MessageBox::Show("Alunno non trovato", "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Error);
} else {
MessageBox::Show(/*name and surname of the student*/, "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
input.close();
I've tried to show the name and surname in different ways: Using a simple sum of string:
if (!tr) {
MessageBox::Show("Alunno non trovato", "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Error);
} else {
string phrase = cognome + " " + none;
MessageBox::Show(phrase, "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
I've tried using c_str:
if (!tr) {
MessageBox::Show("Alunno non trovato", "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Error);
} else {
string phrase = cognome + " " + none;
MessageBox::Show(phrase.c_str(), "Risultato ricerca", MessageBoxButtons::OK, MessageBoxIcon::Information);
}
All of these, give me the error E0304 So I wanted to ask, is there an easy, or better way, to show multiple strings in the MessageBox body?
Upvotes: 0
Views: 239
Reputation: 2057
The problem is that MessageBox::Show
is C++.NET, so its first arguments are of type System::String^
(managed pointer to System::String
) and not std::string
(nor const char *
). You need to convert your string to that type somehow. Try passing gcnew String(phrase.c_str())
.
See https://learn.microsoft.com/en-us/dotnet/api/system.string?view=netframework-4.8 for details.
Upvotes: 1