Solved

Microsoft Visual C++ error  error C2057,error C2133,error C2466

Posted on 2004-10-25
1,663 Views
Last Modified: 2010-07-27
I'm writing very simple code for an array and i get this error...can anyone help? it seems to have to do with my array and the fact that i'm using "double"
 : error C2057: expected constant expression
 : error C2466: cannot allocate an array of constant size 0
 : error C2133: 'b' : unknown size


Here is my code ....what am i doing wrong?

#include <iostream.h>
#include <string.h>
#include <math.h>

int main()
{      

      long n;
      long k;
      double p,q,y;

            cin>> n ;
      cin>> p;

      double b[n+1];

      q = 1 - p;

      y = p/q;

                  b[0] = pow(q,n);

            for (k = 1; k < n;)
            {

                        b[k] = ((n-k+1)*b[k-1]* y )/ k;

            }
            

            return 0;
}


thank you
0
Question by:mrperfect75000
    7 Comments
     
    LVL 6

    Accepted Solution

    by:
    Here's your code with some fixes.Should work fine now :)

    int main()
    {
          using namespace std;
          long n;
          long k;
          double p,q,y;

          cin >> n ;
          cin >> p;

          //double b[n+1];
          double *b;
          b = new double[n+1];

          q = 1 - p;

          y = p/q;

          b[0] = pow(q,n);
          
          for (k = 1; k < n; k++)      //here you probably forgot k++
          {

                b[k] = ((n-k+1)*b[k-1]* y )/ k;

          }


          return 0;
    }
    0
     

    Author Comment

    by:mrperfect75000
    thanks, i get one error now......do you know what it is?
    error C2871: 'std' : does not exist or is not a namespace
    0
     

    Author Comment

    by:mrperfect75000
    thanx a lot - i fixed that......one last question....for some reason it doesnt loop thru K ...gives me just one out put when i cout<<b[k] any ideas?
    0
     
    LVL 6

    Expert Comment

    by:Svetlin_Panayotov
    The only reason I can think of - if n = 1 :)
    0
     

    Author Comment

    by:mrperfect75000
    i set n = 9
    0
     

    Author Comment

    by:mrperfect75000
    Hey thanks i got it...i commented out namespace std becos this problem is caused because certain header files (such as cstdlib and cassert do not declare the std namespace in VC++ 6.0.

    i wasnt gettin good results because i should have put the cout in the loop.
    thanks a lot!!!
    0
     
    LVL 6

    Expert Comment

    by:Svetlin_Panayotov
    You're welcome :)
    Happy coding :)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now