Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 7th, 2004, 8:10 AM   #1
gamehack
Newbie
 
Join Date: Sep 2004
Posts: 12
Rep Power: 0 gamehack is on a distinguished road
Send a message via ICQ to gamehack
Hello all,

Because now I'm converting my friends pascal programs to C++ to show him how much c++ is more powerful compared to pascal I found some difficulties. In pascal, if we have the number pi = 3.141592654 and he issues these statements:
writeln(pi:1:4);
That means that the limit for the number before the . is 1 and after the dot is 4. So it will display 3.1415 In C++ I can use this:
cout.precision(4);
which will print 3.141 So I can only contol the length of the whole number, but I cannot define before the dot and after it. I'm sure that there's some way of doing it, but I cannot find it anywhere... Thanks in advance

Cheers,
gamehack
gamehack is offline   Reply With Quote
Old Oct 7th, 2004, 10:47 AM   #2
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
lookup cout.setf( ). There is a flag in there I believe you set which will cause cout.precision to count using decimal precision only.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Oct 7th, 2004, 3:01 PM   #3
gamehack
Newbie
 
Join Date: Sep 2004
Posts: 12
Rep Power: 0 gamehack is on a distinguished road
Send a message via ICQ to gamehack
So I looked in a book and found the answer. To define the precision right from the decimal you have to set
cout.setf(ios_base::fixed);
Then when you're specifying
cout.precision(n);
It will use this only for the number right from the dot.

Cheers,
gamehack
gamehack is offline   Reply With Quote
Old Oct 7th, 2004, 5:08 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
printf(%1.4f", pi);

Don't know if that's what you want.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 9th, 2004, 5:58 AM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Nah... they're looking for a way to do it using cout. And they've found it, by the looks of things.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC