初学者 c++ 用户在这里我试图从文件中获取行并将其放入字符串数组中。该文件的每个单词都在其自己的行中。每当我测试数组时,它似乎都会将每个字母加载到数组中。所以它是字符而不是每个单词作为字符串。每个文件中的第一件事是一个数字,它将是数组大小。我应该动态分配我相信我做对的数组。欢迎任何帮助。我正在使用 VScode,getline 给我的问题是,没有重载函数“getline”的实例与参数列表匹配——参数类型是:(std::ifstream,char)。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int showMenuGetChoice();
string createStringArray(int);
void getInfoFromUser(string, string, int);
void printStory(int, string, int);
// Calls all the other functions to make a working Madlibs game.
int main ()
{
int UserChoice;
ifstream QuestionFile;
int arraySize;
string Question;
int test = 0;
do {
cout <<"Let's Play some Madlibs!!" << endl << endl;
UserChoice=showMenuGetChoice();
if (UserChoice == 1)
{
ifstream QuestionFile;
QuestionFile.open("starWars.txt");
QuestionFile >> arraySize;
cin.ignore();
string QuestionArray=createStringArray(arraySize);
if(QuestionFile.is_open())
{
QuestionArray[arraySize];
for(int i=0; i < arraySize; i++)
{
getline(QuestionFile, QuestionArray[i]);
}
}
for(int i=0; i < arraySize; i++)
{
cout << QuestionArray[i] << endl;
}
}
// Takes in a integer and creates a array of that size. Dynamically allocates the array and returns a point to this array.
string createStringArray(int n)
{
string*QuestionArray = new string[n];
return *QuestionArray;
}