6

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

The Problem I am currently getting is a error:

"ifstream infile has incomplete type and cannot be defined"

Following is the program:

#include <iostream>              
#include <string>
#include <ifstream>
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
{
    ifstream infile; 
    int counter = 0;
    infile.open(pathname); //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<<")";
}
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
kd7vdb
  • 87
  • 1
  • 1
  • 8
  • 1
    Possible duplicate http://stackoverflow.com/questions/1057287/offstream-error-in-c – Vaibhav Mar 22 '12 at 05:36
  • There's no such standard include file as ``. Your compiler should display an error. If it does not, check its options. You *do* want to have an error in such cases. – atzz Mar 22 '12 at 08:21

2 Answers2

23

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

You should add:

#include <fstream>
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • The new error I get is: no matching function for call to `std::basic_ifstream >::open(std::string&)' – kd7vdb Mar 22 '12 at 13:49
0

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







}
kd7vdb
  • 87
  • 1
  • 1
  • 8