Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何修复导致程序崩溃的错误_C++_Arrays_File Io - Fatal编程技术网

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

C++ 如何修复导致程序崩溃的错误,c++,arrays,file-io,C++,Arrays,File Io,由于某种原因,我无法理解为什么打开时的数据没有放入数组,程序崩溃。我到处寻找答案,但毫无结果。我有一种感觉,在我完成这个程序之前,我会在这里再贴几次 #include <iostream> #include <string> #include <fstream> using namespace std; string bookTitle [14]; string bookAuthor [14]; int loadData (st

由于某种原因,我无法理解为什么打开时的数据没有放入数组,程序崩溃。我到处寻找答案,但毫无结果。我有一种感觉,在我完成这个程序之前,我会在这里再贴几次

#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<<")";
}
#包括
#包括
#包括
使用名称空间std;
字符串书名[14];
字符串图书作者[14];
int loadData(字符串路径名);
void showall(整数计数器);
int main()
{
整数计数器;
字符串路径名;
coutpathname;
加载数据(路径名);
showall(柜台);
cout图书作者[14];

我看到的一个快速问题是:

 infile >> bookTitle [14] ; 
 infile >> bookAuthor [14];
不正确,您需要的是:

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


main()
loadData()
中声明的
计数器
变量是两个不同的变量。在
main()
中声明的一个变量从来都不是反斜体。

我看到的一个简单问题是:

 infile >> bookTitle [14] ; 
 infile >> bookAuthor [14];
不正确,您需要的是:

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


main()
loadData()
中声明的
计数器
变量是两个不同的变量。在
main()
中声明的一个变量从来都不是非斜体。

此代码存在许多问题

主要问题 这些行只写入数组的第14个元素

infile >> bookTitle [14] ;  //takes input and puts into parallel arrays
infile >> bookAuthor [14];
为了填充整个数组,需要使用
计数器
变量作为索引:

infile >> bookTitle [counter] ;  //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
顺便说一句:如果数组大小为14,则最大索引为13。因此您无法访问索引为14的元素。这可能导致崩溃

我注意到的其他问题
  • main()
    中的
    计数器未初始化
  • showall()
    函数接受一个参数但不使用它,它可能是索引
  • showall()
    调用
    main()
    可能需要在循环中才能显示所有元素

此代码存在许多问题

主要问题 这些行只写入数组的第14个元素

infile >> bookTitle [14] ;  //takes input and puts into parallel arrays
infile >> bookAuthor [14];
为了填充整个数组,需要使用
计数器
变量作为索引:

infile >> bookTitle [counter] ;  //takes input and puts into parallel arrays
infile >> bookAuthor [counter];
顺便说一句:如果数组大小为14,则最大索引为13。因此您无法访问索引为14的元素。这可能导致崩溃

我注意到的其他问题
  • main()
    中的
    计数器未初始化
  • showall()
    函数接受一个参数但不使用它,它可能是索引
  • showall()
    调用
    main()
    可能需要在循环中才能显示所有元素

  • 这是一个家庭作业吗?如果是这样,你应该清楚地指出它是什么,你被要求做什么,这样其他的可以帮助你学习而不为你做工作。是的,我唯一的问题是宣布我作为HW的文章是因为一些原因,我有很多人只是发布链接,有基本的C++信息。我知道基本信息问题是我。通常找不到与我的特定问题相关的信息。是的,这就是你学习它的方式:)这是家庭作业吗?如果是,你应该清楚地指出它是,你被要求做什么,其他人可以帮助你学习,而不必为你做这些工作。是的,这是我唯一的问题,宣布我发布的东西为硬件是,出于某种原因,我得到了很多人E,这只是链接的基础上的C++信息。我知道基本信息,问题是我通常找不到与我的特定问题有关的信息。是的,这就是你如何学习它:现在它似乎加载到数组中,但是当它尝试数组时它崩溃了。@ kd7vdB:因为你有大小数组<代码> 14 < /COD>有效数组索引只有
    0
    13
    ,您应该确保您的写入没有超出此边界。如果这样做,您将不会收到任何警告或编译错误,但最终会出现未定义的行为,这可能会使程序崩溃或显示任何rando行为。它现在似乎正在加载到数组中,但当它尝试数组崩溃。@kd7vdb:由于您的数组大小为
    14
    ,有效的数组索引仅为
    0
    13
    ,因此您应该确保您的写操作没有超出此边界。如果这样做,您将不会收到任何警告或编译错误,但最终会出现未定义的行为,可能导致程序或程序崩溃任何rando行为。现在它没有崩溃,我从showall获得了一些输出,但它是十六进制的。这就是当您以
    showall
    中的方式输出数组时会发生的情况。您需要循环数组并单独输出每个项。要找出原因,请搜索“数组指针衰减”。现在它没有崩溃,我从showall获得了一些输出,但它是十六进制的。这就是当您以
    showall
    中的方式输出数组时所发生的情况。您需要在数组中循环并单独输出每个项。要找出原因,请搜索“数组指针衰减”。