Welcome! Log In Create A New Profile

Advanced

How to keep 2 numbers in output

Posted by muskiet 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
How to keep 2 numbers in output
April 01, 2011 10:04PM
Hey there!

I am trying to figure out how to return a double zero entered

You are entering minutes - 00 as int, then need to output it, but only a single 0 comes out
same if you enter 10, in outputs 1 - it disregards the 0...

what can I do to get both the numbers to output and not disregard this

thought on possible solutions:
- set precision - but this only works for decimals
- do I need to convert the int input to char and output as string?
Re: How to keep 2 numbers in output
April 03, 2011 07:54AM
problem is there is no int 00, more logically 00 is not a number - so reading it in as such is moot.

you could read 00 in as a string and then convert to integer 0 to do calculations however this is not straight forward and you may have to put in some extra includes and google some parsing commands ie beyond the scope of the study guide.

I overcame this by reading in integer 0 and at the final stage simply outputted (for example) "00" if the integer was 0. In general prepending a 0 if the integer was between 0-9 worked for me.
avatar Re: How to keep 2 numbers in output
April 03, 2011 02:45PM
This feels a bit kludgy, but how about something on the line of

cout << thisfunc(arg) <<thatfunc(arg) << endl; ?

(Where thisfunc gives you a zero and thatfunc gives you your answer in proper numeric format).

If you don't tell C++ to endl or to "\n" what do you reckon it does?

Please, please, please try to find a better way than this to do this, though. The only virtue here is that you're not converting things to characters (which might be a perfectly good way of doing the job, might it not?).

On second thoughts, maybe you should just convert to character. What's the purpose of the figure? Is it to do further calculations with, or store as a number somewhere? Or is it simply to display to some human what the "answer" is? If it's just an answer, do you think the user would mind if it's Unicode or ASCII or double precision integer?
avatar Re: How to keep 2 numbers in output
April 05, 2011 10:25PM
There's no need to re-invent the wheel, it's all in the STL already. This tutorial shows you nicely what you can do and how to do it.
Re: How to keep 2 numbers in output
April 10, 2011 08:50PM
that would of saved me some time. really got to get into the habit of checking the std lib for stuff like that!
Sorry, only registered users may post in this forum.

Click here to login