views:

100

answers:

2

I've created a variable length array in one function, however I need to refer to this array in a second function. The problem occurs when I put the declaration above main() seeing as its length hasn't been defined yet, my compiler gets angry.

How does one typically go about this?

EDIT:

Here is my code so far.

I need to make the array's name[] midterm[] and final[] global. They're all in student_input().

#include <iostream>
using namespace std ;

void student_input();
void student_output();

int i , ns ;

main(){

    int width,height,mult;

    cout << "Enter the number of students:" << endl;
    cin >> ns;

    i = 0 ;
    while( i < ns){
           i = i + 1 ;
           student_input();
           }

    i = 0 ;
    while( i < ns){
           i = i + 1 ;
           student_output();
           }

    system("pause");  
}

void student_input() {
    int si_i,si_midterm,si_final, midterm[ns + 1], final[ns + 1];
    string si_name, name[ns + 1]; 

    cout <<  endl <<  endl << "\t----- Student " << i << " -----" << endl << endl << endl;

    cout << "Enter name for student " << i << ":\t"<< endl;
    cin >> si_name;
    name[i] = si_name ; 

    cout << "Enter midterm score for student " << i << ":\t"<<  endl;

    cin >> si_midterm;
    midterm[i] = si_midterm ;

    cout << "Enter final exam score for student " << i << ":\t"<<  endl ;
    cin >> si_final;
    final[i] = si_final ;

    cout <<  endl <<  endl;

    si_i = 0 ;
    while (si_i < 7){
          si_i = si_i + 1; 
          cout << "Enter lab " << si_i <<" for student " << i << ":\t"<<  endl;
          }

    cout << name[i] <<  endl << midterm[i] <<  endl<<final[i] <<  endl;      
    return;
}

void student_output() {
    cout <<"hello! "<<  endl;
    return;
}
+13  A: 

C++ does not support variable length arrays; either you are not using C++ or you are using an implementation-specific language extension.

In C++ you should use std::vector for a dynamically sized array.

If you need to access it from multiple functions you can:

  • have the functions that need access to the vector take a reference to it as an argument, or
  • make the vector a class member variable and make all the functions that need to access it member functions of the class.

Which one makes more sense depends on what, exactly, you are trying to do.

James McNellis
@james Can you please explain, "C++ does not support VLA but compiler allows it as an extension" I do understand c++ doesnt support VLA, but the following statemet can you please explain a bit more?
Als
@Als: What exactly needs further explanation? C++ doesn't support VLAs. C99 does. If the OP is actually using C++ _and_ VLAs then his compiler must support VLAs as a language extension.
James McNellis
I'm using Bloodshed Dev-c++, for reference.
Anteater7171
Als
@Als: Most (all?) compilers support nonstandard language extensions and few (no?) compilers fully adhere to the language standards. I don't have a problem with language extensions if they can be turned off or are turned off by default.
James McNellis
@James: Thanks! Any other such usual/Important extensions that Most(all?) compilers provide as an language extension? Good to know, so can avoid using them.
Als
@Als: In `gcc/g++`, non-standard extensions can be prevented by compiling with `-ansi -pedantic` option. See: http://www.network-theory.co.uk/docs/gccintro/gccintro_28.html
ArunSaha
+6  A: 

What's wrong with std::vector? You cannot have a VLA in C++ (g++ provides it as an extension though).

Prasoon Saurav