Expected Constant Expression error-C++

Discussion in 'C++' started by ibleedart, Mar 23, 2007.

  1. ibleedart

    ibleedart New Member

    Joined:
    Mar 23, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I have searched for help on this on the internet, but I'm not quite sure why I can't use a variable as the array size in this one. I read that C99 compilers can accept this and this is how my C++ textbook says to do this, so I'm having trouble locating the problem. I am running Visual C++ 2005 Express Edition, but I am new to this and don't know what compiler this Visual C++ has so there is a chance the compiler isn't a C99. Is there any other way to get this code to compile with a variable as the array size? The line with the error "expected constant expression" starts off with the declaration and initialization:
    Code:
    int studentScores[numOfScores]={0};
    
    I even tried an initialization array instead of this at one point, but it just caused more errors with the same "expected constant expression" as the first error. Since it isn't that long, I've posted the complete code. If any more information or clarification is needed, don't hesitate to ask and thank you in advance for the help.

    -Andrea

    Code:
    #include <iostream>
    
    using namespace std;
    
    void fill(int stuScores[], int numScores);
    
    int main()
    {
    	int numOfScores=26;
    	int range1=0, range2=0, range3=0, range4=0, range5=0, range6=0, range7=0, range8=0;
    	int studentScores[numOfScores]={0};
    	int count;
    
    	cout<<"Please enter"<<numOfScores<<"test scores:";
    
    	fill(studentScores[], numOfScores);
    
    	for(count=0; count<numOfScores; count++)
    	{
    		if(studentScores[count]<0)
    		{
    			cout<<"Invalid Score"<<endl;
    		}
    		else
    			if(studentScores[count]<=24)
    			{
    				range1++;
    			}
    			else
    				if(studentScores[count]<=49)
    				{
    					range2++;
    				}
    				else
    					if(studentScores[count]<=74)
    					{
    						range3++;
    					}
    					else
    						if(studentScores[count]<=99)
    						{
    							range4++;
    						}
    						else
    							if(studentScores[count]<=124)
    							{
    								range5++;
    							}
    							else
    								if(studentScores[count]<=149)
    								{
    									range6++;
    								}
    								else
    									if(studentScores[count]<=174)
    									{
    										range7++;
    									}
    									else
    										if(studentScores[count]<=200)
    										{
    											range8++;
    										}
    										else
    											if(studentScores[count]>200)
    											{
    												cout<<"Invalid Score"<<endl;
    											}
    	}
    
    	cout<<"The score ranges and the number of students are:"<<endl;
    
    	cout<<"0-24:"<<" "<<range1<<endl;
    	cout<<"25-49:"<<" "<<range2<<endl;
    	cout<<"50-74:"<<" "<<range3<<endl;
    	cout<<"75-99:"<<" "<<range4<<endl;
    	cout<<"100-124:"<<" "<<range5<<endl;
    	cout<<"125-149:"<<" "<<range6<<endl;
    	cout<<"150-174:"<<" "<<range7<<endl;
    	cout<<"175-200:"<<" "<<range8<<endl;
    
    	return 0;
    }
    
    void fill(int stuScores[], int numScores)
    {
    	int index;
    	for(index=0; index<numScores; index++)
    	{
    		cin>>stuScores[index];
    	}
    }
    
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    VC2005 is a C/C++ compiler, but not C99 compliant. Few are. Dev-C++ will accept your method (it uses MinGW). I would advise you, at least for a while, to use dynamic allocation or move to C++ and use a container such as those provided in the STL.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice