•
•
•
•
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
![]() |
•
•
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,562
Reputation:
Rep Power: 8
Solved Threads: 158
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.
do NOT pm me for help, it makes me angry. You wouldn't like me when I'm angry...
•
•
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.
Member of: Beautiful Code Club.
•
•
Join Date: Apr 2008
Posts: 110
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;
Member of: Beautiful Code Club.
•
•
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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