1
string degreesToDMS(double angle) {
    int intpart = 0;
    int intpart2 = 0;
    int intpart3 = 0;
    return floor(angle) << "\xb0" << modf(angle, &intpart)*60 << "'" << modf(modf(angle, &intpart2), &intpart3)*60 << "\"";
}

This function takes in an angle in degrees and outputs a latitude.

I am getting errors on the return statement. How do I properly concatenate different data types to a string in C++?

3
  • 4
    Start by creating a string object. Commented Oct 3, 2012 at 16:33
  • string::append should help as well. Commented Oct 3, 2012 at 16:33
  • possible duplicate of C++ how to add more strings into a method Commented Oct 3, 2012 at 19:21

4 Answers 4

3

If you want to use the streaming operators then use a std::stringstream, like this:-

string degreesToDMS(double angle)
{
  int intpart = 0;
  int intpart2 = 0;
  int intpart3 = 0;
  stringstream ss;
  ss << floor(angle) << "\xb0" << modf(angle, &intpart)*60 << "'" << modf(modf(angle, &intpart2), &intpart3)*60 << "\"";
  return ss.str ();
}
Sign up to request clarification or add additional context in comments.

6 Comments

ss is allocated on the stack isn't it a problem?
@0x90 Where else should it be allocated (and why)?
@0x90 ss is allocated on the stack, but the call to ss.str() returns an std::string object by value that is then returned by the function.
@Prætorian And even if ss.str() would return a reference to its internal string the function degreesToDMS itself is still declared to return a string by-value.
@cRaZiRiCaN: That is a linker error, and each line will tell you what symbol was not found. You need to compile/link the definitions for those symbols into your program.
|
2

You need to first build the result in an std::ostringstream and then retrieve the string from it.

std::ostringstream ss;
ss << floor(angle) << "\xb0" << modf(angle, &intpart)*60 ...
return ss.str();

There are other ways of achieving this result; for instance, with C++11 you can use std::to_string to convert the values to std::string and then concatenate them together.

return std::to_string(floor(angle)) + "\xb0" + 
         std::to_string(modf(angle, &intpart)*60) + ...

Comments

1
std::string result;
result += std::to_string(floor(angle);
result += "\xb0";
result += std::to_string(modf(angle, &intpart) * 60);
return result;

Note that this requires C++11 to get std::to_string.

Comments

1

To concatenate a string in C++ all you need to do is use the + operator on two strings.

If you want to convert a int to a string use the stringstream

#include <string>
#include <sstream>
using namespace std;

int main()
{
    string firstString = "1st, ";
    string secondString = "2nd ";

    string lastString  = firstString + secondString;

    int myNumber = 3;

    std::stringstream converANumber;
    converANumber << myNumber;

    lastString = lastString + converANumber.str();

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.