User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,609 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,620 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 293 | Replies: 7
Reply
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation: edman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Conversion from int to double

  #1  
Jul 8th, 2008
I am trying to convert the ouput of this recursive function to double, but no matter what I try the output remains integer. Could someone please assist.

int main()
{
	int x,y;
	cout << "Please enter the low value of the range to add: " << endl;
	cin >> x;
	cout << "Please enter the high value of the range to add: " << endl;
	cin >> y;
	cout << "The sum of the range is: " << static_cast<double>(addRange(x,y)) << endl;
	cin.get();//To keep console window open
	cin.get();//To keep console window open
	return 0;
}

double addRange(int x, int y)
{
	if (x > y)	
		return (0);
	else
		return static_cast<double>(addRange(static_cast<double>(x) + 1,y)) + x;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,562
Reputation: niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough niek_e is a jewel in the rough 
Rep Power: 8
Solved Threads: 158
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Virtuoso

Re: Conversion from int to double

  #2  
Jul 8th, 2008
Why all the horrible casting?
Why not just change the function to take in doubles?
double addRange(double x, double y)
{
Last edited by niek_e : Jul 8th, 2008 at 10:46 am.
do NOT pm me for help, it makes me angry. You wouldn't like me when I'm angry...
Reply With Quote  
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation: edman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Re: Conversion from int to double

  #3  
Jul 8th, 2008
This is an assignment question and we were spcifically asked to return a double from this recursive function that passes two int parameters. It is senseless, I know :-)
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Conversion from int to double

  #4  
Jul 8th, 2008
>no matter what I try the output remains integer
If you perform an operation on two integers that wouldn't have any precision even if converted to a double, why would you expect anything but a zero precision? No amount of casting will create something that isn't there.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Apr 2008
Posts: 110
Reputation: ivailosp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 21
ivailosp ivailosp is offline Offline
Junior Poster

Re: Conversion from int to double

  #5  
Jul 8th, 2008
  1. #include <iostream>
  2. using namespace std;
  3. double addRange(int x, int y);
  4. int main() {
  5. int x, y;
  6. cout << "Please enter the low value of the range to add: " << endl;
  7. cin >> x;
  8. cout << "Please enter the high value of the range to add: " << endl;
  9. cin >> y;
  10. cout << "The sum of the range is: " << addRange(x, y) << endl;
  11. cin.get();//To keep console window open
  12. cin.get();//To keep console window open
  13. return 0;
  14. }
  15.  
  16. double addRange(int x, int y) {
  17. if (x > y)
  18. return (0);
  19. else
  20. return (addRange(x + 1, y)) + x;
  21. }
lol, works fine...
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Conversion from int to double

  #6  
Jul 8th, 2008
>lol, works fine...
Yes, but I imagine the OP wants the precision to be printed as well. Input of 1 and 5 should print 15.000000 rather than 15, for example. To do that has nothing to do with the type and everything to do with how it's printed:
  1. cout<<"The sum of the range is: "<< fixed << addRange(x, y) <<endl;
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation: edman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
edman edman is offline Offline
Newbie Poster

Re: Conversion from int to double

  #7  
Jul 8th, 2008
Thank you for the help. I agree there is no point to the conversion exercise except to teach me something else I didn't know.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,663
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 16
Solved Threads: 297
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Conversion from int to double

  #8  
Jul 8th, 2008
This sounds pretty silly to me too.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

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

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:11 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC