0

我正在使用指针开发一个 madlibs 程序。当我尝试正确构建它时,但我认为动态分配的数组用于存储读取的文本文件中的行存在一些问题。数组中的数字是文件中的哨兵值。我还留在 cout 语句中以显示它存储错误之前的信息。有什么帮助吗?错误是“条目”周围的堆栈。

//这是我到目前为止的代码

#include <iostream>       
#include <string>         
#include <fstream>
#include <sstream>




using namespace std;

void header();
//string play_again();
void read_game_file(string **entries, int *num_entries, string **story, int *num_lines);
//string get_user_input(string* entries, int * num_entries);

int main()
{

    header();
    cout<<endl<<endl;

    int num_entries=(NULL);
    int *num_lines=(NULL);
    string *entries (NULL);
    string *story (NULL);

    read_game_file( &entries,  &num_entries,  &story,   &*num_lines);
    cout<<"doneszo"<<endl;
    string get_user_input(string*entries, int * num_entries);


}

void header()
{
     cout<<"Hello! Welcome to the game Madlibs."<<endl;
     cout<<"The object of the game is to produce something that sounds totally ridiculous."<<endl;
     cout<<"So don't think to hard about your answers."<<endl;
     cout<<"At the top, you will see a bunch of word descriptions followed by blank spaces."<<endl;
     cout<<"Type your word in the blanks. The words should match the descriptions on the left."<<endl;
     cout<<"Enter no when you no longer wish to play. Enter yes to continue. Have a great laugh!"<<endl;
 }

void read_game_file(string **entries, int *num_entries, string **story, int *num_lines)
{
     //Ask user to input file name and opens file
     ifstream mad_lib;
     string file_name;

     cout<<"Please enter the file name with extension: ";
     cin>>file_name;
     mad_lib.open(file_name);

     //Checks to see that file name is valid if not ask for input again
     if (!mad_lib)
     {
        cout<<"File could not be opened. Please try again"<<endl;
        cout<<"Please enter the file name with extension: ";
        cin>>file_name;
        mad_lib.open(file_name);
        }

     int work;
     string line;
     mad_lib>>work;
     num_entries=&work;
     getline(mad_lib,line);

     *entries=new string[*num_entries];
     cout<<*num_entries<<endl;
     string * entry;
     for(int i=0; i<*num_entries; i++)
     {
         entry = new string;
         getline(mad_lib,*entry);
         entries[i]= entry;
         cout<<*(entries[i])<<endl;
     }


     string work_2;
     int work_3;
     stringstream ss;
     getline(mad_lib,work_2);
     ss<<work_2;
     ss>>work_3;
     cout<<work_2<<endl;
     num_lines=&work_3;
     *story=new string[*num_lines];
     string *entry_2;

     for(int j=0; j<=*num_lines; j++)
     {
         entry_2=new string;
         getline(mad_lib,*entry_2);
         story[j]= entry_2;
         cout<<*(story[j])<<endl;
     }

}
4

1 回答 1

0

除非必要,否则不要在函数参数中使用指针。

请改用按引用传递和 const。

另一个要问自己的问题——你真的想read_game_file在它的论点中“填充”一些数据吗?你能设计得更好吗?在这里,您在 中定义了一堆变量main,并且您希望read_game_file为您“填充”它们。

相反,您应该将此功能封装在一个类中。将文件名作为参数传递给类的ReadFile(const string& filename)方法。所有这些数据都应该在类中。

要解决您的直接问题(以不干净的方式):

void read_game_file(string **entries, int *num_entries, string **story, int *num_lines);

应该

void read_game_file(vector<string>& entries, int& num_entries, vector<string>& story, int& num_lines);


int main()
{

    header();
    cout << endl << endl; // spaces help readability!

    int num_entries = 0;
    int num_lines = 0;
    vector<string> entries;
    vector<string> story;

    read_game_file( &entries,  &num_entries,  &story,   &*num_lines);
    cout << "doneszo"<< endl;
    string get_user_input(vector<string>& entries, int& num_entries);
}

使用向量而不是数组。它们优雅而干净,易于使用。我把这个功能留给你完成。

于 2013-10-18T05:59:27.097 回答