Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,359 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,184 people online right now. Registration is fast and FREE... Join Now!




Loop compile error

 
Reply to this topicStart new topic

Loop compile error

zerogee
post 22 Aug, 2008 - 08:56 PM
Post #1


New D.I.C Head

Group Icon
Joined: 20 Aug, 2008
Posts: 42

Again I am having trouble compiling this program. it is just the input codes only. These are the codes that are causing the errors.

CODE
char* name[MAX_COUNT_STUDENT];
    int exam_1[MAX_COUNT_STUDENT];
    int exam_2[MAX_COUNT_STUDENT];
    int HomeWork[MAX_COUNT_STUDENT];
    int Final_Exam[MAX_COUNT_STUDENT];


I get the same 3 errors for all 5 lines when compiling. the errors are;

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'Final_Exam' : unknown size

This is the complete code:
CODE
#include <iostream>
#include <string>

using namespace std;

int main()
{
      
    int i=0, MAX_COUNT_STUDENT = 3;
        
    char* name[MAX_COUNT_STUDENT];
    int exam_1[MAX_COUNT_STUDENT];
    int exam_2[MAX_COUNT_STUDENT];
    int HomeWork[MAX_COUNT_STUDENT];
    int Final_Exam[MAX_COUNT_STUDENT];
    
    for(i=0; i < 3; i++)
    {
        
         for (i=0; i < MAX_COUNT_STUDENT; i++)
            cout << "Enter Student Name: ";
            cin >> name[i];
        
        
            cout << "\nEnter Exam_1_Grade: ";
            cin >> exam_1[i];
        
        
            cout << "\nEnter Exam_2_Grade: ";
            cin >> exam_2[i];
        
        
            cout << "\nEnter HomeWork_Avg: ";
            cin >> HomeWork[i];
        
        
            cout << "\nEnter Final_Exam_Grade: ";
            cin >> Final_Exam[i];
        

     }
    
    return 0;
}
User is offlineProfile CardPM

Go to the top of the page

joske
post 23 Aug, 2008 - 12:16 AM
Post #2


D.I.C Head

**
Joined: 4 Sep, 2007
Posts: 156



Thanked 11 times
My Contributions


MAX_COUNT_STUDENT has to be a contant. So, change the declaration to:
CODE
    int i=0;
    const int MAX_COUNT_STUDENT = 3;


furthermore, you have to change the declaration of name to string. (a pointer to a char does not do what you intend to do).
CODE
string name[MAX_COUNT_STUDENT];



And lastly, you forgot the block parentheses for the second for loop.

Here the changed code:
CODE
#include <iostream>
#include <string>

using namespace std;

int main()
{
      
    int i=0;
    const int MAX_COUNT_STUDENT = 3;
        
    string name[MAX_COUNT_STUDENT];
    int exam_1[MAX_COUNT_STUDENT];
    int exam_2[MAX_COUNT_STUDENT];
    int HomeWork[MAX_COUNT_STUDENT];
    int Final_Exam[MAX_COUNT_STUDENT];
    
    for(i=0; i < 3; i++)
    {
        for (i=0; i < MAX_COUNT_STUDENT; i++)
        {
        cout << "Enter Student Name: ";
        cin >> name[i];
        
        
        cout << "\nEnter Exam_1_Grade: ";
        cin >> exam_1[i];
        
        
        cout << "\nEnter Exam_2_Grade: ";
        cin >> exam_2[i];
        
        
        cout << "\nEnter HomeWork_Avg: ";
        cin >> HomeWork[i];
        
        
        cout << "\nEnter Final_Exam_Grade: ";
        cin >> Final_Exam[i];
        }
     }
    
    return 0;
}
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month