1

I am trying to create a console app in C++ that prompts the user to enter a floating point number and then takes that number and separates out the integer part and the fraction part.

Example output would be:-

Please enter a floating point number:
800.589
The integer part is 800 and the fraction part is .589

My solution is shown below:

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double anyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
fractionPart = fmod(anyNumber,1);
integerPart = anyNumber - fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " << *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double anyNumber = 0;
cin >> anyNumber;
cout << endl;    
spliceAnyNumber(anyNumber);
system("Pause");
return 0;   
}

I wrote the program but I am also being asked to pass pointers to the function and manipulate the dereferenced values. I tried to do that below but I am getting a bunch of errors back from the compiler.

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
double integerPart = 1;
double fractionPart = 1;
double *pIntegerPart = &integerPart;
double *pFractionPart = &fractionPart;
&fractionPart = fmod(&anyNumber,1);
&integerPart = &anyNumber - &fractionPart;
cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
cout << endl;
cout << "The address of *pIntegerPart is " << &integerPart << "\n";
cout << endl;
cout << "The address of *pFractionPart is " << &fractionPart << "\n";
cout << endl;
}

int main()
{   
cout << "Please enter a floating point number: ";
double *pAnyNumber = &anyNumber;
cin >> *pAnyNumber;
cout << endl;    
spliceAnyNumber(*pAnyNumber);
system("Pause");
return 0;   
}

Where am I going wrong with adding in pointers? Version 1 works but version 2 does not.

5
  • 4
    Read your compiler's error messages one at a time, and ask if you can't figure out exactly what one of them is complaining about. Commented Jan 30, 2013 at 21:19
  • 1
    I believe that what you're expected to do is implement void spliceAnyNumber (double anyNumber, int* integerPart, int* fractionalPart), and return the integer part in the integerPart parameter, and the fractional part in the fractionalPart parameter. It's a common way to "return" more than one value from a function. Commented Jan 30, 2013 at 21:24
  • @molbdnilo Common, but crap style! Commented Jan 30, 2013 at 21:25
  • @molbdnilo: In C++ "out-parameters" are typically modelled as references if they cannot be avoided. Commented Jan 30, 2013 at 21:27
  • @bitmask I know that, but this exercise explicitly called for pointer manipulation. Personally, I loathe non-const references and avoid them whenever possible. Commented Jan 30, 2013 at 21:33

4 Answers 4

1

I've notated this inline.

#include <iostream>
#include <cmath>
using namespace std;

void spliceAnyNumber (double *pAnyNumber)
{
  double integerPart = 1;
  double fractionPart = 1;
  double *pIntegerPart = &integerPart;
  double *pFractionPart = &fractionPart;
  &fractionPart = fmod(&anyNumber,1);  // <- you should dereference pAnyNumber instead, and assign to fractionPart (i.e. "fractionPart = fmod(*pAnyNymber, 1);
  &integerPart = &anyNumber - &fractionPart;  // <- similar as above
  cout << "The integer part is " << *pIntegerPart << " and the fraction part is " <<     *pFractionPart << "\n"; *pFractionPart << "\n";
  cout << endl;
  cout << "The address of *pIntegerPart is " << &integerPart << "\n";
  cout << endl;
  cout << "The address of *pFractionPart is " << &fractionPart << "\n";
  cout << endl;
}

int main()
{   
  cout << "Please enter a floating point number: ";
  double *pAnyNumber = &anyNumber;  // <- you haven't declared an 'anyNumber' variable to take the address of
  cin >> *pAnyNumber;
  cout << endl;    
  spliceAnyNumber(*pAnyNumber);
  system("Pause");
  return 0;   
}
Sign up to request clarification or add additional context in comments.

Comments

0

The & operator takes the address of a variable, so typeof(&anyNumber) == double**. You want the * operator instead.

You should read double *pAnyNumber as "When I apply the * operator, I get a double". (You actually get an lvalue reference, but that doesn't roll off the tongue and will probably confuse you...)

Your main function is a mess; leave it the same as the original and change spliceAnyNumber(pAnyNumber); to spliceAnyNumber(&pAnyNumber);.

Comments

0

I assume when you wrote anyNumber you actually meant pAnyNumber. If you have a pointer

double* p;

You dereference by *p, not &p. The former gives you a double while the latter gives you a double**.

Comments

0

You have to declare anyNumber before you can dereference it:

double *pAnyNumber = &anyNumber; // references an undeclared variable

Just take the address when passing to the function. Before that, you can use normal variables - no need for pointers:

double anyNumber;
cin >> anyNumber;
cout << endl;
spliceAnyNumber(&anyNumber); 

Additionally, you're using the wrong operator in your function. It should be like this:

*pFractionPart = fmod(*pAnyNumber,1);
*pIntegerPart = *pAnyNumber - fractionPart;

The other thing is invalid syntax: &variable = ... literally means "address of variable = ", which results in a double**.

So the only change you have to make is the function parameter, and accessing it. No need for all those pointers inside the function..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.