#include <string>
#include <iostream>
#include <fstream>
using namespace std;
bool DMS(string &input)
{
int position;
string degrees = " \0xf8";
string minutes = " \"";
string seconds = " '";
const string degreeList[4] =
{ " DEGREES", " DEGREE", "DEGREES", "DEGREE"};
const string minuteList[4] =
{ " MINUTES", " MINUTE", "MINUTES", "MINUTE"};
const string secondList[4] =
{ " SECONDS", " SECOND", "SECONDS", "SECOND"};
// Find and replace DEGREES
for(int i = 0; i < 4; ++i)
{
position = input.find(degreeList);
while(position != string::npos)
{
input.replace(position, degreeList.length(), degrees);
position = input.find(degreeList, position + 1);
}
}
// Find and replace MINUTES
for(int i = 0; i < 4; ++i)
{
position = input.find(minuteList);
while(position != string::npos)
{
input.replace(position, minuteList.length(), minutes);
position = input.find(minuteList, position + 1);
}
}
// Find and replace SECONDS
for(int i = 0; i < 4; ++i)
{
position = input.find(secondList);
while(position != string::npos)
{
input.replace(position, secondList.length(), seconds);
position = input.find(secondList, position + 1);
}
}
}
int main()
{
ofstream fout;
fout.open("C:\\Test.txt");
string a = "DEGREE MINUTESSECONDS MINUTE DEGREES";
fout << a << endl;
DMS(a);
fout << a;
cin.get();
fout.close();
return 0;
}
Small Problem
I'm having a problem with a function that I am writing. It scans through a string finding and replacing certain words with a designated symbol. Here's the code and the main test code:
It's not printing the degree sign for the degrees. Could someone plz tell me what i am doing wrong?
That's because you're sending "\0xf8", which is a four character string that starts with null terminator - in other words, it may as well be a blank string.
You could just do:
string degrees = "°";
or if that doesn't work, (might not due to different charsets) something like:
string degrees = " ";
degrees[0] = 0xf8;
You could just do:
string degrees = "°";
or if that doesn't work, (might not due to different charsets) something like:
string degrees = " ";
degrees[0] = 0xf8;
Actually... " \xf8" might be what you were aiming for? (See http://www.cppreference.com/escape_sequences.html).
If not, Sandman's second solution seems like the most compatible -- although 0xf8 is still only guaranteed to be the degree symbol in ASCII. (Shouldn't be a problem since you're working with C++ strings anyway)
If not, Sandman's second solution seems like the most compatible -- although 0xf8 is still only guaranteed to be the degree symbol in ASCII. (Shouldn't be a problem since you're working with C++ strings anyway)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement