【发布时间】:2016-03-08 18:16:20
【问题描述】:
所以我必须键入一个大学课程菜单,该菜单使用字符串和数组提出一系列问题。
我目前已经输入了这个。
`
#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' 可能未初始化错误,我真的无法指出为什么它不会:\ 非常感谢。
【问题讨论】:
-
这是什么:
void populateArray(string * statesArray, int & sizeOfArray) int statesArray[] = {}; {...? -
如果可以使用
std::vector,为什么还要使用可变大小的数组扩展? -
由于您已经在使用向量,因此将
statesArray设为向量。您不能创建大小在编译时未知的普通数组。 -
它应该给一些大学
标签: c++