Infile incomplete type error

后端 未结 2 1132
渐次进展
渐次进展 2021-01-18 06:31

I am building a program that takes an input file in this format:

title author

title author

etc

and outputs to screen 

title (author)

title (author)

etc         


        
相关标签:
2条回答
  • 2021-01-18 07:15

    File streams are defined in the header <fstream> and you are not including it.

    You should add:

    #include <fstream>
    
    0 讨论(0)
  • 2021-01-18 07:18

    Here is my code with the previous error fixed Now I get a problem of the program crashing after I input the name of the text file.

    #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);
    }
    
    
    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 [14];  //takes input and puts into parallel arrays
               infile >> bookAuthor [14];
               counter++;
         }
    
         infile.close();
    }
    
    void showall (int counter)        // shows input in title(author) format
    {
    
         cout<<bookTitle<<"("<<bookAuthor<<")";
    
    
    
    
    
    
    
    }
    
    0 讨论(0)
提交回复
热议问题