2016-03-08 92 views
0

所以我必须输入一个大学程序菜单,它使用字符串和数组来提出一系列问题。可变大小的对象数组可能无法初始化

我现在打印了这个。

`

#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(); 



} 

它不断给我“可变大小的物体‘statesArray’可能无法初始化错误,我真的不能点我的,为什么它不会手指:\得益于先进

+1

这是什么:void populateArray(string * statesArray,int&sizeOfArray) int statesArray [] = {}; {...'? – user463035818

+2

为什么使用可变大小的数组扩展如果你可以使用'std :: vector'? – Jarod42

+2

由于您已经在使用vector,所以'statesArray'是一个向量。你不能创建一个在编译时不知道大小的普通数组。 – NathanOliver

回答

0

变长数组(VLA的)是c++非标准所以这行也不行。

string statesArray [sizeOfArray] = {}; 

关于为什么有一个合理的好解释here。至于你的例子,由于你已经在使用std::vector,你应该简单地用std::vector<std:string>代替statesArray