|
-
February 3rd, 2009, 04:29 PM
#1
C++ newbie help needed
Code:
#include <iostream>
#include <cmath>
using namespace std;
// Stirling's Formula :::: e^(-n) * n^(n) sqrt(2)(PI)(n)
// n = 18
const int PI = 3.14;
double n;
double r;
int main()
{
int stirling1, stirling2;
cout << "Enter a number for n! : ";
cin >> n;
"Enter a number for r! : ";
cin >> r;
for (n; n > 1; n++)
{
stirling1 = n;
stirling2 = r * (n-r);
double CalcStirling = stirling1 / stirling2;
cout << "Stirling's string is : " << CalcStirling << endl;
return 0;
} }
All the syntax is good, it just doesnt do what I want to do.
Im trying to have n be 18 and calculate the factorial of that and r be 6 and have the factorial calculated. But it will not calculate. I type any # in for "n" and it shows it, but then you type in anything for "r" and it just returns 0.
Can anyone help me here so that it lets you input a value for both "n" and "r" and then returns the right result ?
Im VERYY new to C++ so please forgive me . Thanks
-
February 3rd, 2009, 04:49 PM
#2
Re: C++ newbie help needed
stirling1 and stirling2 are ints. Integer division truncates so you get 18 / 72 = 0.
-
February 4th, 2009, 02:36 AM
#3
Re: C++ newbie help needed
you need to turn stirling1 and stirling2 into float numbers, OR do a typecast
BTW: the curly brackets for the for loop is missing.
-
February 7th, 2009, 07:49 AM
#4
Re: C++ newbie help needed
PI should be a FLOATING POINT constant variable and shouldn't exist in your source code
Last edited by richard_tominez; February 7th, 2009 at 07:56 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|