I need to convert from a double value to an architectural string format. EG:
25.875 ->> 25'-10 1/2"
To do this, I have written the following code, but I am not sure about it although it seems to work.
I thought I would post the code for any possible improvement suggestions:
Code Removed!
On second thought, I will attach a CEdit derived class I use that converts the value from a real (double) to a string (in imperial) as required.
Take a look if you are interested.
its ugly I know!
Any suggestions.
Mike B
Last edited by MikeB; October 23rd, 2003 at 03:28 PM.
You then call this function whenever you want an architectural string, regardless of whether it is going to a CEdit, the console window, a file, or to the printer.
#include <string>
#include <sstream>
#include <iostream>
std::string ArchitectureFormat(double d)
{
std::ostringstream strm;
int numft = (int)d;
double frac = d - (double)numft;
double dInches = frac * 12.0;
int nInches = (int) dInches;
// the fractional part for inches was not done. This is a little bit harder to do. But here is the output.
// code to convert
strm << numft << "'-" << nInches << "\"";
return strm.str();
}
int main()
{
std::cout << ArchitectureFormat(25.875);
}
This is just a starting point. There are things left out, but this is much simpler than what you are attempting to do. You just need to fill in the gaps in the function I provided.
Originally posted by MikeB
It is not really an assertion I don' t think, it is "Unhandled exception in GeneralFunctions.exe: 0XC0000005 Access Violation"
OK, so it is not an assertion failure but an access violation...
Now while I could of course go through your code and figure out where and why it fails or copy it to a local project and compile it and try to run it to see what happens - it would be easier if you would provide that information. Just run your code in the debugger and tell us on which line it stops when the access violation occurs.
Oops, I just wanted to reply that your call to strm.str() was missing the parentheses... I admit that I didn't find that out by looking at the code, but by running your code in the debugger. Actually, it doesn't fail on the line you indicated, but inside the implementation of strlen. Walking up the callstack brings you to the implementation of std::string, where you could see that the 'this' pointer is invalid, indicating that ArchitecturalFormat() returned something else than a valid string object.
However, I wonder why the compiler didn't choke at this (after all, ArchitecturalFormat() was supposed to return a std::string object, but it returned a function pointer (the address of strm.str()). Probably has to do with some overloaded pointer operator in std::string, but I'm to lazy to look into that now... One of the reasons why I avoid using std::string whenever possible, and use CString instead.
Originally posted by gstercken
However, I wonder why the compiler didn't choke at this (after all, ArchitecturalFormat() was supposed to return a std::string object
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.