CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: C++ newbie help needed

    stirling1 and stirling2 are ints. Integer division truncates so you get 18 / 72 = 0.

  3. #3
    Join Date
    Jan 2009
    Posts
    24

    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.

  4. #4
    Join Date
    Mar 2008
    Posts
    30

    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
  •  





Click Here to Expand Forum to Full Width

Featured