c++ - 如何修复导致程序崩溃的错误

标签 c++ arrays file-io

出于某种原因,我无法弄清楚为什么打开时数据没有放入数组中,并且程序崩溃了。我四处寻找答案无济于事。我觉得在我完成这个程序之前我会在这里多发几次!

#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<<")";
}

编辑:任务是获取名称格式为标题的文件 作者 标题 作者 然后它必须以标题(作者)格式将数组输出到屏幕。

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

代码现在是这样的;

#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()中声明的counter变量是两个不同的变量。在 main() 中声明的那个根本就不会被初始化。

关于c++ - 如何修复导致程序崩溃的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9833534/

相关文章:

c# - 如何将字节数组转换回文件并使用 C# 自动打开它?

c++ - 在 std::list 和 std::vector 之间进行选择

java - Android (ART) 崩溃并出现错误 JNI DETECTED ERROR IN APPLICATION : jstring is an invalid local reference

c++ - 如何声明采用已知模板参数类型的模板模板参数的模板函数?

javascript - array.pop()的返回值;

java - 使用方法输出到用户决定的文本文件

C++11 允许对非静态和非常量成员进行类内初始化。发生了什么变化?

javascript - 将文档属性中的数组与 dataRange 中的数组进行比较不起作用

c - 在调查中提取 winner/-s,然后打印

Java PrintWriter 不写入文件