I am just trying to load a data file into an array and display it. I run it and nothing comes up on the screen. Are my arrays set the right way and set to be output the right way. Please let me know what I am missing.

int main()
{
    //Declare vars
    string Title[150];
    int Year[150];
    int Length[150];
    string Media[150];

    int sub = 0;
//  int compares = 0;
//  int passes = 0;
//  int exchanges = 0;

    ifstream dataIn;    //declare an instance variable WITHOUT attaching an
                        //actual file

    string filename;
    cout << "Enter the data file name (and path): ";
    getline(cin, filename);

    //Open file
    //dataIn.open("C:\\NamesAndAges.dat");
    dataIn.open(filename.c_str());
    if(dataIn.fail() == true)   //same as if(dataIn.fail())
    {
        cout << "Unable to access the data file." << endl;
        return 888;
    }

    //Load arrays with data from record
    dataIn >> Title[sub] >> Year[sub] >> Length[sub] >> Media[sub];
    while(dataIn.eof() == false)  //same as while(!dataIn.eof())
    {
        sub = sub + 1;
        dataIn >> Title[sub] >> Year[sub] >> Length[sub] >> Media[sub];
    }

    dataIn.close();

    int maxElements = sub;  //ACTUAL number of elements in the arrays
                            //that contain data

    cout << "\n\nArray contents.." << endl << endl;
    cout << "Title                                 Year     Length       Media" << endl;
    cout << "----------------------------------------------------------------------" << endl;

    //Display the arrays contents
    for(sub = 0; sub < maxElements; sub++)
    {
        cout << left << setw(25) << Title[sub]
             << setw(25) << Year[sub]
             << right << setw(10) << Length[sub] << setw(10) << Media[sub] <<endl;
    }

}

Recommended Answers

All 5 Replies

Have you tried to debug the program ?

Have you tried to debug the program ?

How? With what? Am I missing something big? It runs just closes once I run it. I think it is because of the commas.. But then I did it differently with getline and it still doesnt work and I even tried making my int arrays into string..I dont know

U posted some incomplete code there..

setw is an undeclared function you are attempting to use..
also DataIn is aggregated for whatever reason I do not know.. U don't even have headers in your code..

I included <iostream> <windows.h> <string>
but that only got rid of some of the errors not all..

dataIn >> Title[sub] >> Year[sub] >> Length[sub] >> Media[sub];

this should be:

dataIn >>Title>>Year>>Length>>Media;

because when you say dataIn>>Title[sub]; it means: "extract character from the file and store it in the Title array at position sub".

Anyway what are you trying to achieve with this program??

I gave you a (almost) complete solution for the load part in the other post you made on the same topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.