| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Allan Bruce" |
| Date: |
16 Oct 2003 07:58:17 AM |
| Object: |
Re: This is homework but I need advice please |
"minndream" <member42700@dbforums.com> wrote in message
news:3405816.1065316089@dbforums.com...
this is homework and I do not want an answer but if somebody could give
me a little nudge in the right direction I would be very grateful.
This is what I am supposed to do:
Write a program that promps for and reads a floating-point value. The
program prints the whole part on one line and the decimal part on the
second line example: 23.45
23
45
This is what I have so far:
#include <iostream>
#include <string>
using namespace std;
int main(){
float a = 0;
double b = 0;
cout<<"Enter a 4 digit number example 23.45: "<< endl;
cin>>a>>b;
cout<<a<<"\n."<<b<< endl;
return 0;
}
again, I don't want the answer but I am missing something.
Thanks so much!!!
I would look into casting the float into an integer, e.g.
float userInput;
int wholePart = (int)userInput;
and then easy for the reamainder, just take away the wholePart from the
userInput using another cast.
HTH
Allan
.
|
|
| User: "Karl Heinz Buchegger" |
|
| Title: Re: This is homework but I need advice please |
16 Oct 2003 08:37:31 AM |
|
|
Allan Bruce wrote:
I would look into casting the float into an integer, e.g.
float userInput;
int wholePart = (int)userInput;
Note: technically you don't need the cast.
Just assigning the floating point value to an int variable
is sufficient. Some (most) compilers will give a warning
when doing so (something about: hey man, you are loosing
some precission if you do this), and then you insert
the cast to shut up the compiler. But from the point of
C++ the cast is not neccessary.
int wholePart = userInput;
and then easy for the reamainder, just take away the wholePart from the
userInput using another cast.
Why a cast?
float fraction = userInput - wholePart;
BTW: Unless you know what you do and you have a very
good reason, don't use float. Use double instead!
--
Karl Heinz Buchegger
kbuchegg@gascad.at
.
|
|
|
|

|
Related Articles |
|
|