0

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;
}
kd7vdb
  • 87
  • 1
  • 1
  • 8
  • is this homework? if so, you should clearly indicate it is and what you were asked to do so other can help you learn without doing the work for you. – cbrulak Mar 23 '12 at 02:56
  • yes it is, My only problem with declaring something I post as HW is that for some reason I get alot of people that just post links that have basic c++ info. I know the basic info the problem is I usually can't find info pertaining to my particular question. – kd7vdb Mar 23 '12 at 03:09
  • yeah, that's how you learn it :) – cbrulak Mar 23 '12 at 16:23

2 Answers2

2

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • It Now seems to be loading into the arrays but when it try's to cout the arrays it crashes. – kd7vdb Mar 23 '12 at 03:06
  • @kd7vdb: Since you have array of size `14` the valid array indices are only `0` to `13`, You should ensure that You are not writing beyond this boundary.If you do so, You wont get any warnings or compilation errors but You would end up with a Undefined Behavior which might crash your program or show any rando behavior. – Alok Save Mar 23 '12 at 03:19
1

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.
grigy
  • 6,696
  • 12
  • 49
  • 76
  • Now it isn't crashing and I am getting some output from showall but it is in hex. – kd7vdb Mar 23 '12 at 03:28
  • 1
    That's what happens when you output arrays the way you do in `showall`. You need to loop through the arrays and output each item individually. To find out why, search for "array pointer decay". – molbdnilo Mar 23 '12 at 08:41