如何更正运行时错误变量周围的堆栈已损坏?

时间:2013-10-18 05:43:13

标签: c++ arrays runtime

我正在使用指针处理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;
     }

}

1 个答案:

答案 0 :(得分:0)

在必要之前,不要在函数参数中使用指针。

使用传递引用和const代替。

要问自己的另一个问题 - 你真的希望read_game_file来填充&#34;他的论点中的一些数据?你能更好地设计吗?在这里,您在main中定义了一堆变量,并期望read_game_file到&#34;填充&#34;他们适合你。

您应该将此功能封装在一个类中。将文件名作为参数传递给类的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);
}

使用向量而不是数组。它们优雅而干净,易于使用。 我将离开这个功能让你完成。