Reputation:
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++?
Upvotes: 1
Views: 875
Reputation: 9841
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();
}
Upvotes: 1
Reputation: 109089
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) + ...
Upvotes: 2
Reputation: 76245
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
.
Upvotes: 1
Reputation: 71060
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 ();
}
Upvotes: 3