Sign in to follow this  
NightStalker

Small Problem

Recommended Posts

NightStalker    122
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
Sandman    2210
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

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