![]() |
Is there a better way to do this?
I'm recreating whatever things from algebra/trigonometry that I can think of as a learning exercise. I wanted to post the whole thing for feedback but I don't know how that'd go in this board (it is for support after all).
This function turns a degree measure into degrees, minutes, and seconds. It works fine but I want to know if there's a better way to put the results in one place rather than three stringstreams and three strings. I want the function to return a string because there are three numbers as an answer. :
|
Re: Is there a better way to do this?
The use of stringstreams, in this instance, amounts to a presentation issue. You have to deal with that, make the conversions, fiddlefart around, and convert back to presentation.
Your code doesn't look half bad, but you're expecting all your operations to work properly and magically. Get a life. Things don't always work out. Test for success and take appropriate actions if something fails. Anything less is the action of an unemployable newbie or a schlock. Choose your category. Your potential employer is entitled to disagree. |
Re: Is there a better way to do this?
Personally I'd probably create a structure to house the three separate values and return that from the function. Stringing them together to return just means you end up with an amalgamated text string that you would then probably need to deconstruct to use elsewhere.
|
Re: Is there a better way to do this?
Some people might think this is overcomplicated, but if you have Boost I like the Tuple library. It's specifically designed to make returning multple values from a function easier.
:
I copied your algorithm, but converted it to use static_cast's instead. I can't comment on the algorithm itself. :) |
Re: Is there a better way to do this?
Quote:
Quote:
Quote:
Jesse: Boost is nice (although I haven't gotten it to work yet) but I want to make something I can carry with me anywhere. |
Re: Is there a better way to do this?
Having done a bit of websurfing (sorry I use C# so didn't want to post code as my syntax would probably be a bit off) this site
http://www.cplusplus.com/doc/tutorial/classes.html Seemed to have some good information on what I was meaning, create a class, with three int's in it (Degrees, Minutes and Seconds) and then have the function return your new class as it's result. |
Re: Is there a better way to do this?
Well, I understand what you mean, but you can't return a class instance as a function result. Jesse's answer is basically what I want, but I was hoping for a solution within the standard headers.
edit: By the way, Jesse, I tried out the boost solution. It gave errors so I played around with the code (I've never used tuples or boost so it was guesswork) till it was satisfied: :
Can't cout<<deg2dms(x); though, apparently there's no << for tuples. |
Re: Is there a better way to do this?
Quote:
:
Which defines an operator<< that outputs things in the form :
(x y z)But if you really want to do it with standard headers, you should just make a custom struct and return it -- basically what Arla suggested. |
Re: Is there a better way to do this?
Quote:
:
:
$ ./exampleYou are copying the instance on the return from the function (which does add overhead), but the same thing happens with Boost.Tuple. The only reason to use Boost.Tuple in this case in convenience. |
Re: Is there a better way to do this?
Quote:
Oops, operator<< slipped my mind.:
|
| All times are GMT -5. The time now is 3:15 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC