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 427,939 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,772 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: Programming Forums
Views: 440 | 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,780
Reputation: niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all niek_e is a name known to all 
Rep Power: 11
Solved Threads: 185
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.
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
do NOT pm me for help, in the best case, you'll get ignored
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,343
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 Narue has much to be proud of 
Rep Power: 29
Solved Threads: 462
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Apr 2008
Posts: 123
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,343
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 Narue has much to be proud of 
Rep Power: 29
Solved Threads: 462
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;
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
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,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
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.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
Reply With Quote  
Reply

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

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

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