how does one create two functions that get used in main?

closed account (j1AR92yv)
How does one go about extracting the whole part of an integer and in another function, the decimal?

Last edited on
Are you asking how to write functions?
If yes, then see http://www.cplusplus.com/doc/tutorial/functions/


If you just wonder how to split a value, then ask yourself what happens in:
1
2
double foo = ...
int bar = foo;


(There is a little twist. There are also functions in <cmath>, but you won't need them.)
closed account (j1AR92yv)
Hey, yeah basically trying to write these two functions.
For the int integer part, I'm trying to separate the whole from the decimal.
Which i did this...
int integer(double number)
{
cin >> number;
int number2;
number2 = number2 - number;
return number;


}

------

int integer(double number)
{
double result;
return number;


}

Though, it's giving me
warning C4244: '=': conversion from 'double' to 'int', possible loss of data
warning C4244: 'return': conversion from 'double' to 'int', possible loss of data
error C2084: function 'int integer(double)' already has a body

Though I feel my function is doing the opposite....it's getting the decimal part. I think.
The first version has issues:
1
2
3
4
5
6
7
8
9
10
11
int integer( double number )
{
  cin >> number; // this overwrites the value of number
  // this function should compute something from its parameter
  // no I/O

  int number2; // uninitialized. Unknown value
  number2 = number2 - number; // unknown - something is unknown

  return number; // you did compute something into number2, but return number
}


The second is better:
1
2
3
4
5
int integer( double number )
{
  double result; // unused variable
  return number; // only a warning about conversion
}


The warning is about cases when the double has a value that is larger than what the int can store.
You should comment that your function will fail with such input.

The compiler does warn about the conversion, because it does not know whether you wrote it intentionally or by accident. Explicit conversion creates no warning:
static_cast<int>(number)
Topic archived. No new replies allowed.