1

I have the following function in C++:

std::wstring decToDeg(double input) { int deg, min; double sec, sec_all, min_all; std::wstring output; sec_all = input * 3600; sec = Math::Round(static_cast<int>(sec_all) % 60, 3); //code from @spin_eight answer min_all = (sec_all - sec) / 60; min = static_cast<int>(min_all) % 60; deg = static_cast<int>(min_all - min) / 60; output = deg + L"º " + min + L"' " + sec + L"\""; return output; } 

When I try to compile I get this error:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion) 

What can I do to correct these two errors in my function?

EDIT: solved

std::wstring decToDeg(double input) { int deg, min; double sec, sec_all, min_all; sec_all = input * 3600; sec = Math::Round(static_cast<int>(sec_all) % 60, 3); min_all = (sec_all - sec) / 60; min = static_cast<int>(min_all) % 60; deg = static_cast<int>(min_all - min) / 60; std::wostringstream output; output << deg << L"º " << min << L"' " << sec << L"\""; return output.str(); } 
6
  • 1
    sec_all is double, the error message is pretty clear. Commented Nov 22, 2012 at 7:36
  • Also, you're trying to sum numbers with strings, which can't be done like this in C++ Commented Nov 22, 2012 at 7:37
  • Could you tell me what to do to solve these errors? I am totally new to C++... Commented Nov 22, 2012 at 7:40
  • Look at stackoverflow.com/questions/64782/… for various ways to fix #2 Commented Nov 22, 2012 at 7:46
  • 1
    This isn't C++. It's C++/CLI. Commented Nov 23, 2012 at 14:15

4 Answers 4

1

You could use a string stream to construct output, like this:

std::wostringstream output; output << deg << L"º " << min << L"' " << sec << L"\""; return output.str(); 
Sign up to request clarification or add additional context in comments.

1 Comment

@Victor What error? BTW, of course you have to remove your original declaration of output as a wstring, and you have to #include <sstream>.
1
sec = Math::Round(static_cast<int>(sec_all) % 60, 3); 

1 Comment

output = std::to_string(deg) + L"º " +std::to_string(min) + L"' " + std::to_string(sec) + L"\"";
1

You cannot use modulo on doubles. Modulo on doubles:

 int result = static_cast<int>( a / b ); return a - static_cast<double>( result ) * b; 

Comments

0

For the first error try this:

 min = (static_cast<int>(min_all) ) % 60; 

This should make sure the cast will be completed first, before trying to do any other calculating.

If your other error is not an error that was caused by the first error, you may want to try using stringstream. It behaves like a normal I/O stream and is thus ideal for formatting strings.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.