-2

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)));

}
AIV
  • 47
  • 5

2 Answers2

3

You are actually coding in C++/CLI rather than C++. And you are trying to convert from a .net managed ref string to a C++ string. Do that like so:

#include <msclr/marshal_cppstd.h>

std::string RefStringToNativeString(System::String^ s)
{
    return msclr::interop::marshal_as<std::string>(s);
}

After that, you are then faced with converting a double to a managed ref string. Well, let's assume you can convert the double to a C++ string. Then you need:

System::String^ NativeStringToRefString(const std::string& s)
{
    return gcnew System::String(s.c_str());
}

You can bypass the second function like this:

txtAverage->Text = 
    prosjek(RefStringToNativeString(txtWithNumbers->Text)).ToString();

If you really are going to use C++/CLI then you perhaps might as well use the .net string rather than the C++ string to avoid all this back and forth.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • So the call should look like this: txtAverage->Text = NativeStringToRefString(prosjek(RefStringToNativeString(txtWithNumbers->Text))); – AIV Apr 01 '14 at 22:10
  • Nearly. You still have to convert the return value of `prosjek()` from double to `std::string`. Or convert it directly using `System::String.Format`. – David Heffernan Apr 01 '14 at 22:12
  • Like this: double pr=suma/brojac; return pr.ToString; And ofc string prosjek(....){...} – AIV Apr 01 '14 at 22:15
  • For instance, `d.ToString()` or `System::String::Format("{0}", d)` – David Heffernan Apr 01 '14 at 22:17
  • I receive this error message: 5 IntelliSense: no suitable constructor exists to convert from "System::String ^" to "std::basic_string, std::allocator>" – AIV Apr 01 '14 at 22:20
  • Well, I've no idea what your code looks like and which line leads to that. The code in my answer all works. – David Heffernan Apr 01 '14 at 22:22
  • I'll post the revision in my question edit. – AIV Apr 01 '14 at 22:24
  • Please use the exact code that is in my answer! I edited it. You must also try and understand that `std::string` is not the same as `System::String^`. Your latest edit shows that you still don't really get that. In the change to `prosjek`, the way the return value is returned, you make the exact same mistake as you originally made. – David Heffernan Apr 01 '14 at 22:27
  • OK, works now, thanks a lot, now I just need to fix the function itself, doesn't do what it's meant :D – AIV Apr 01 '14 at 22:31
  • OK, good. Next time you ask make sure that you include the c++-cli tag because otherwise you will get lots of confused c++ answers. I know this is your first post which is why I am trying to steer you in the right direction. – David Heffernan Apr 01 '14 at 22:32
  • I had no idea I was writing in c++-cli hahahah, thanks again :D – AIV Apr 01 '14 at 22:33
  • I guess you use C++/CLI because you want to use WinForms or WPF GUI. If you want to build any sort of software in C++/CLI you will need to adopt a policy as to when you use managed ref types, and when you use unmanaged C++. You'll get in a real twizzle if you mix and match in an ad hoc fashion. It's not a language I have any experience in at all so I've no useful advice as to what the policy should be. But my experience tells me that such a policy is essential!! – David Heffernan Apr 01 '14 at 22:35
2

Try:

txtAverage->Text = prosjek(txtWithNumbers->Text->ToString());
//                                                      ^^^^
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084