本文介绍了如何修复导致程序崩溃的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,我不知道为什么数据打开时不会被放入数组和程序崩溃。我已经搜索了一个答案没有效果。我得到的感觉,我会在这里发布几次,在我完成这个程序之前!

For some reason or another I cannot figure out why the data when opened does not get put into arrays and the program crashes. I have searched around for a answer to no avail. I get the feeling I will be posting a here a few more times before I finish this program!

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string bookTitle [14];
string bookAuthor [14];
int loadData (string pathname);
void showall (int counter);

int main ()
{
    int counter;
    string pathname;

    cout<<"Input the name of the file to be accessed: ";
    cin>>pathname;
    loadData (pathname);
    showall (counter);

    cout<<"Press <Enter> to Exit";
    cin.ignore();
    cin.get();
    return 0;
}

int loadData (string pathname) // Loads data from infile into arrays
{
    fstream infile;
    int counter = 0;
    infile.open(pathname.c_str()); //Opens file from user input in main
    if( infile.fail() )
    {
        cout << "File failed to open";
        return 0;
    }
    while (!infile.eof())
    {
        cout<<"File Opened";   // I get the "File Opened" text and then a crash
        infile >> bookTitle [14] ;  //takes input and puts into parallel arrays
        infile >> bookAuthor [14];
        cout<<"Data Put in Arrays";
        counter++;
    }

    infile.close();
}

void showall (int counter)        // shows input in title(author) format
{
    cout<<bookTitle<<"("<<bookAuthor<<")";
}

编辑:分配是取一个文件名,格式如title
author
title
author
然后它必须以标题(作者)格式输出数组到屏幕。

The assignment is to take a file with names in a format such as title author title authorThen it must output the arrays to screen in a title(author) format.

之后,我必须使它成为一个循环程序,所以可以从一个数组中选择一个搜索,并以标题(作者)格式返回条目。

After this I must make it a looping program so a search can be chosen of one of the arrays and returns the entry in a title(author) format.

代码现在如何;

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

string bookTitle [50];
string bookAuthor [50];
int loadData (string pathname);
int showall (int counter);

int main ()

{
string pathname;
int counter=0;

cout<<"Input the name of the file to be accessed: ";
cin>>pathname;
loadData (pathname);



showall (counter);

cout<<"Press <Enter> to Exit";
cin.ignore();
cin.get();
return 0;
}


int loadData (string pathname) // Loads data from infile into arrays
{
    fstream infile;
    int counter = 0;
    infile.open(pathname.c_str()); //Opens file from user input in main
    if( infile.fail() )
     {
         cout << "File failed to open";
         return 0;
     }

     while (!infile.eof())
     {

           infile >> bookTitle [counter] ;  //takes input and puts into parallel arrays
           infile >> bookAuthor [counter];
           counter++;
     }

     infile.close();
}

int showall (int counter)        // shows input in title(author) format
{

     cout<<bookTitle<<"("<<bookAuthor<<")";






return 0;
}


推荐答案

 infile >> bookTitle [14] ;
 infile >> bookAuthor [14];

不正确,您需要的是:

 infile >> bookTitle[counter];
 infile >> bookAuthor [counter];

此外,

main() loadData()中声明的$ c> counter 不同的变量。在 main()中声明的一个不会被初始化。

The counter variable declared in main() and in loadData() are two different variables. The one declared in main()is never iniitalized at all.

这篇关于如何修复导致程序崩溃的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:28