•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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;
}•
•
Join Date: Oct 2006
Location: the Netherlands
Posts: 1,780
Reputation:
Rep Power: 11
Solved Threads: 185
Why all the horrible casting?
Why not just change the function to take in doubles?
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
do NOT pm me for help, in the best case, you'll get ignored
•
•
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
>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.
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.
•
•
Join Date: Apr 2008
Posts: 123
Reputation:
Rep Power: 1
Solved Threads: 21
cpp Syntax (Toggle Plain Text)
#include <iostream> using namespace std; double addRange(int x, int y); 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: " << 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 (addRange(x + 1, y)) + x; }
>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:
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:
cplusplus Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Conversion from variables to arrays (C++)
- Matrix Determinate Calculation (C++)
- conversion from string to double error (VB.NET)
- void pointers (C++)
- conversion (C++)
- arrays and functionality of the program... (C++)
- New 2 C++, help with finding erros (C++)
- Pounds to Grams Conversion (C++)
Other Threads in the C++ Forum
- Previous Thread: Convert ANSI characters to character
- Next Thread: stingstream and assigning fields of csv files to variables



Linear Mode