this is my first question here. So, I'm trying to make a function which gets numbers from a textbox written like this "5 5 4 2" and separates them to individual ints and then calculates the average. What I've done so far is this:
double prosjek(string a)
{
string razmak = " "; //the string separator, space between the numbers
string token = a.substr(0, a.find(razmak)); //finds those separators in the textbox
size_t pos = 0; //sets the position to zero
int suma=0; //initializes the sum
int brojac=0; //initializes the counter
while ((pos = a.find(razmak)) != std::string::npos) { //loops through string and separates the numbers
token = a.substr(0, pos);
int numb;
istringstream ( token ) >> numb;
suma+=numb;
a.erase(0, pos + razmak.length());
brojac++;
}
double prosjek=suma/brojac;
return prosjek; //returns the average
}
I have no idea how to call this function for a specific textbox. I've tried this:
txtAverage->Text=prosjek(txtWithNumbers->Text->ToString);
But I get this error message from IntelliSense: Error 1 error C3867: 'System::String::ToString': function call missing argument list; use '&System::String::ToString' to create a pointer to member
Edit:
The updated code (still needs fixes):
string RefStringToNativeString(System::String const^ s)
{
return msclr::interop::marshal_as<std::string>(s);
}
String^ NativeStringToRefString(const std::string& s)
{
System::String^ result = gcnew System::String(s.c_str());
return result;
}
string prosjek(string a)
{
string razmak = " ";
string token = a.substr(0, a.find(razmak));
size_t pos = 0;
int suma=0;
int brojac=0;
while ((pos = a.find(razmak)) != std::string::npos) {
token = a.substr(0, pos);
int numb;
istringstream ( token ) >> numb;
suma+=numb;
a.erase(0, pos + razmak.length());
brojac++;
}
double pr=suma/brojac;
return pr.ToString();
}
private: System::Void btnIzrPr_Click(System::Object^ sender, System::EventArgs^ e) {
txtAverage->Text = NativeStringToRefString(prosjek(RefStringToNativeString(txtWithNumbers->Text)));
}