![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
hi: for(;;) goto hi;
|
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. c++ Syntax (Toggle Plain Text)
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 323
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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.
c++ Syntax (Toggle Plain Text)
I copied your algorithm, but converted it to use static_cast's instead. I can't comment on the algorithm itself. ![]()
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 | |||
|
hi: for(;;) goto hi;
|
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.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. |
|||
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Mar 2005
Posts: 323
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
hi: for(;;) goto hi;
|
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: c++ Syntax (Toggle Plain Text)
Can't cout<<deg2dms(x); though, apparently there's no << for tuples.
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. Last edited by peaceofpi; Nov 25th, 2007 at 1:11 AM. |
|
|
|
|
|
#8 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Re: Is there a better way to do this?
Quote:
c++ Syntax (Toggle Plain Text)
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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
|
#9 | |
|
hi: for(;;) goto hi;
|
Re: Is there a better way to do this?
Quote:
Oops, operator<< slipped my mind. c++ Syntax (Toggle Plain Text)
__________________
How do you play Religious Roulette? Stand around in a circle and blaspheme till someone gets struck by lightning. Last edited by peaceofpi; Nov 25th, 2007 at 12:23 PM. |
|
|
|
|
|
|
#10 | |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
Re: Is there a better way to do this?
Quote:
c++ Syntax (Toggle Plain Text)
$ ./example (33, 5, 2) You 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.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|