Sign in to follow this  

Small Problem

This topic is 4531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
#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[i]);
      while(position != string::npos)
      {
         input.replace(position, degreeList[i].length(), degrees);
         position = input.find(degreeList[i], position + 1);
      }      
   }
   // Find and replace MINUTES
   for(int i = 0; i < 4; ++i)
   {
      position = input.find(minuteList[i]);
      while(position != string::npos)
      {
         input.replace(position, minuteList[i].length(), minutes);
         position = input.find(minuteList[i], position + 1);
      }      
   }
   // Find and replace SECONDS
   for(int i = 0; i < 4; ++i)
   {
      position = input.find(secondList[i]);
      while(position != string::npos)
      {
         input.replace(position, secondList[i].length(), seconds);
         position = input.find(secondList[i], 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;
}


It's not printing the degree sign for the degrees. Could someone plz tell me what i am doing wrong?

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

This topic is 4531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this