How do I read data such as song titles from a file and store them in an array?
I have:

const int SIZE = 50;
int main()
{
    char theFile[SIZE];
cout << "Please enter a file name: " << endl;

cin.getline(theFile, SIZE);
ifstream theFile(" ");

...

Hmm,
Think I figured it out, and yes i have to use an array not string:

const int SIZE = 50;
int main()
{
    char theFile[SIZE];
    ifstream userData;

cout << "Please enter a file name: " << endl;
cin.getline(theFile, SIZE);
userData.open(theFile);

Okay still need help then alphabetizing the input. We didn't really go over this, and I couldn't find much in the text. How do I organize the input alphabetically?

>>How do I organize the input alphabetically?
there are at least two ways I can think of:
1. use a linked list, insert the new name in alphabetical order. The program will have to search all the nodes in the list to find the right spot.
2. similar to #1 above, but use a dynamically expanding array of strings instead of a linked list.
3. Find out how many names are in the file then allocate an array of pointers of that size. Read each string into the array without regard to alphatetizing. After file has been read sort the array using on of many sorting algorithms.

#3 above is the one I would prefer because I think it is the fastest. Not very much searching involved, which can be pretty slow with large arrays.

Hmm, how would i use isalpha(aChar) ?

like:

int main()
{
    char theFile[SIZE];
    ifstream userData;

    cout << "Please enter a file name: " << endl;
    cin.getline(theFile, SIZE);
    userData.open(theFile);

    int i = 0;
    while(i < SIZE && isspace(theFile[i]))
    {
    i++;
    }
    if (i == SIZE)
        return 0;
    std::cout << (char)toupper(theFile[i++]);
    std::cout << (char)isalpha(theFile[i++]);

variable theFile contains the name of a file on disk, it does not contain the information that is in the file. the open function will just open up the file so that the program can read it -- the open function does not actually read the file. To do that you need to either use the stream's extraction operator >> or call its read() function. What is the purpose of all that code after the open() function? It appears to be completly useless.

isalpha() just checks to see if a character is alphabetic -- that is if the character is one of 'a' to 'z' or 'A' to 'Z'. any other character will fail the test.

It is part of what else I have to do, such as capitalize the first letter. I was hoping to get the alphabetical part right, but obviously I was wrong.

Can I use:

while (theFile >> aSong)
{
cout << "Songs: " << aSong);
}

Hmm, I am supposed to use a two dimensional array, which of course I'm not sure how to do except i need to use ascii maybe?

Hmm, I am supposed to use a two dimensional array, which of course I'm not sure how to do except i need to use ascii maybe?

here is the 2d character array. define MAXTITLES and MAXTITLELENGTH whatever value you wish.

char titles[MAXTITLES][MAXTITLELENGTH];

after opening the file, enter a loop and read each line until end-of-file. If song titles contain spaces you will have to use infile.getline(). Something like this:

char titles[MAXTITLES][MAXTITLELENGTH];
char name[MAXTITLELENGTH];
int nTitle = 0;
while( infile.getline(name,MAXTITLELENGTH))
{
   strcpy(titles[nTitle++],name);
}

Okay I get this to compile, but I must not be converting the file to 'theFile' correctly, because when I enter the file name it just hangs. What have i got wrong?

#include <iostream>
#include <fstream>

using namespace std;
const int SIZE = 50;
const int MAXTITLES = 10;
const int MAXTITLELENGTH = 50;

int main()
{
    char theFile[SIZE];
    char songs[MAXTITLES][MAXTITLELENGTH];
    int nTitle =  0;
    cout << "Please enter a file name: " << endl;
    cin.getline(theFile, SIZE);
    ifstream userData;
    userData.open(theFile);
    
    while (userData.getline(theFile, MAXTITLELENGTH))
    {
        strcpy(songs[nTitle++], theFile);
    }

    cout << songs;
    
cin.get();
system("PAUSE");
return 0;
}

>>cout << songs;
that won't work -- you have to create a loop and output each one individually.

cin.get();
system("PAUSE");

you only need one of the two above lines, not both. Suggest you delete tye system() function call.

>>What have i got wrong?
looks ok to me. put a print statement inside that loop so that you can see what its doing.

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.