0

So I have to type a college program menu that asks a series of questions using strings, and arrays.

I have this typed up currently.

`

    #include <iostream>

    #include <fstream>

    #include <vector>

    #include <string>

using namespace std;

vector<string> populateVector();
void populateArray(string *, int &);
void menu();

int main()
{
    vector<string> collegesVector = populateVector();
    int sizeOfArray = collegesVector.size();
    string statesArray [sizeOfArray] = {};
    populateArray(statesArray, sizeOfArray);

    cout << "\nWELCOME TO MY COLLEGE AND UNIVERSITY SUMMARY PROGRAM."
    cout << "\n Press 1 to enter possible colleges and universities and the program will tell you if they appear on the list."

    system("pause>nul");


do
{
    cout << "\nPress 1 to enter possible colleges and universities and the program will tell you if they appear on the list.";
    cout << "\nPress 2 to find out how many colleges and universities appear in the state of your choice.";
    cout << "\nPress 3 to find out how many colleges and universities appear on our list in total.";
    cout << "\nPress 4 to quit";

    if (choice == 1)
{
  do
  {
      cout << "Please enter the name of your college or university. ";
      cin >> userInput;

      collegesVector(resemblance);

      if(userInput != resemblance)
        cout << "\nThe institution you've entered is not on the list.\n";
      else
        cout << "\nThe institution you've entered is on our list!.\n";



  }
}

}


vector<string> populateVector()
{
    string marker;
    string entry;
    vector<string> collegesVector;
    fstream inputFile;
    inputFile.open("colleges.txt");
    if (!inputFile)
    {
        cout << "cannot read";
        return vector<string>();
    }

    getline(inputFile, marker);

    while(!inputFile.eof())
    {
        getline(inputFile, entry);

        if(entry.length() > 0)
        {
            collegesVector.push_back(entry);
        }

    }
    inputFile.close();
    return collegesVector;
}
void populateArray(string * statesArray, int & sizeOfArray)
int statesArray[] = {};
{
    fstream inputFile;
    string marker;
    string entry;
    inputFile.open("states.txt");
    if (!inputFile)
    {
        cout << "cannot read";
        return;
    }
    getline(inputFile, marker);

    for(int i = 0; i < sizeOfArray; i++)
    {

        getline(inputFile, entry);

        if(entry.length() > 0)
        {
            statesArray[i] = entry;
        }
    }
   inputFile.close();



}

It keeps on giving me the "Variable-sized object 'statesArray' may not be initialized error and I really cant point my finger on why it wont :\ thanks in advanced.

Kev
  • 11
  • 1

1 Answers1

0

Variable Length Arrays (VLA's) are non standard in c++. So this line is not ok:

string statesArray [sizeOfArray] = {};

There is a reasonably good explanation here as to why. As for your example, since you are already using std::vector you should simply replace statesArray with a std::vector<std:string>.

Community
  • 1
  • 1
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175