kd7vdb
kd7vdb

Reputation: 87

How to fix Error that leads to program Crash

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

Edit: The assignment is to take a file with names in a format such as title author title author Then 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.

Here is how the code stands now;

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

Upvotes: 0

Views: 276

Answers (2)

grigy
grigy

Reputation: 6826

There are many problems with this code.

Main problem

These lines write only into 14th elements of the arrays

infile >> bookTitle [14] ;  //takes input and puts into parallel arrays
infile >> bookAuthor [14];

In order to fill the entire array you need to use the counter variable for index:

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

BTW: if array's size is 14 then the maximum index is 13. So you can't access an element with index 14. And that's probably causes the crash.

Other problems I noticed

  • counter in main() is not initialized
  • showall() function takes an argument but does not use it, which is probably the index.
  • showall() call in main() probably needs to be in a loop, to show all elements.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206508

A Quick problem I see is:

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

Is incorrect, What you need is:

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

Also,

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

Upvotes: 2

Related Questions