I'm having some problems with my code:

int a, b,x, c, d, cdnr ;
    char songname[4][3000][30];
    char  sok[4], avsok[30], cdtitel[4][30], r[30], i;
{  ifstream array2;
  array2.open ("array.txt");
      array2 >> i;
for(a=1;a<=i;a++){
array2>>cdtitel[a];
array2>>r[a];
for(b=1;b<=r[a];b++)
array2>>songname[a][b];
    
}
array2.close();
}

for(a=1;a<=i;a++){
cout<<cdtitel[a]<<endl<<endl;

for(b=1;b<=r[a];b++)
cout<<songname[a][b]<<endl;


}

My text file:

3
AAAAAAAA
2
111
222
BBBBBBBBBBb
3
111
222
333
CCCCCCCCCCccc
4
111
222
333
444

The structure is:

Number of CDs
CD title 1
Number of songs
Song name 1
Song name 2
CD title 2
etc...

Output:

AAAAAAAA

2
111
222
BBBBBBBBBBb
3
111
222
333
CCCCCCCCCCccc
4
111
222
333
444

+101 empty lines

The actual output should be:

AAAAAAAA

111
222

BBBBBBBBBBb

111
222
333

CCCCCCCCCCccc

111
222
333
444

It should show the cd title and then the song names, second cd title and song names etc.

I really can't see where the problem is...
Any help would be greatful

Why don't you just use the principle of reading and writing structures from/to a file?

To write to the file:
Let's say we want to write the following structure to a file:

struct book {
    char author[30];
    char title[20];
    char publisher[20];
    int price;
};
const int NUM_OF_BOOKS = 30;
ofstream outfile;
book library[NUM_OF_BOOKS];
outfile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!outfile.is_open())
{
    cerr << "ERROR: Couldn't write file." << endl;
    exit(1);
}
// ... we assume that there are already some values in 'library'
outfile.write(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
outfile.close();

To read from the file:
Reading the structures back from the file is analog:

const int NUM_OF_BOOKS = 30;
ifstream infile;
book library[NUM_OF_BOOKS];
infile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!infile.is_open())
{
    cerr << "ERROR: Couldn't read file." << endl;
    exit(1);
}
infile.read(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
infile.close();

Hope this helps ! :)

One problem, you're mixing data types.

char  ....... , i;
{  ifstream array2;
  array2.open ("array.txt");
      array2 >> i;
for(a=1;a<=i;a++){

You read in to the character i, getting the character '3'.
When you use i as the for loop limit, it gets typecast to an integer value, its ASCII value. That explains why your loop is running too far - this isn't really seen on the input, but you see the result on the display loop.

Change i to be an int.

One problem, you're mixing data types.

char  ....... , i;
{  ifstream array2;
  array2.open ("array.txt");
      array2 >> i;
for(a=1;a<=i;a++){

You read in to the character i, getting the character '3'.
When you use i as the for loop limit, it gets typecast to an integer value, its ASCII value. That explains why your loop is running too far - this isn't really seen on the input, but you see the result on the display loop.

Change i to be an int.

Thx. Completely missed that.
The amount of empty lines reduced, but the data is still displayed incorrectly. No difference from before except less lines.

Any idea where the problem could be?

Why don't you just use the principle of reading and writing structures from/to a file?

To write to the file:
Let's say we want to write the following structure to a file:

struct book {
    char author[30];
    char title[20];
    char publisher[20];
    int price;
};
const int NUM_OF_BOOKS = 30;
ofstream outfile;
book library[NUM_OF_BOOKS];
outfile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!outfile.is_open())
{
    cerr << "ERROR: Couldn't write file." << endl;
    exit(1);
}
// ... we assume that there are already some values in 'library'
outfile.write(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
outfile.close();

To read from the file:
Reading the structures back from the file is analog:

const int NUM_OF_BOOKS = 30;
ifstream infile;
book library[NUM_OF_BOOKS];
infile.open("[I][U]yourfilename[/U][/I]", ios::binary);
if(!infile.is_open())
{
    cerr << "ERROR: Couldn't read file." << endl;
    exit(1);
}
infile.read(reinterpret_cast<char* >(library),NUM_OF_BOOKS*sizeof(book));
infile.close();

Hope this helps ! :)

Hmm, interesting. I'm new to C++, so not very familiar with your coding.

How exactly would you display the read data? Let's say I use array.txt with your coding, how would I display author, title etc..

Thanks

Same problem with your array r[] - you make it type char, you read in something you mean to use as the integer value.

If your variable names had more meaning, you might see these places where data type doesn't match purpose/type of value contained.

How exactly would you display the read data?

(I assume the data is already read into the library structure)
To display the whole library you could make use of a for-statement like this:

for(int i=0; i<NUM_OF_BOOKS; i++)
{
     cout << "Author: " << library[i].author << endl;
     cout << "Title: " << library[i].title << endl;
     cout << "Publisher: " << library[i].publisher << endl;
     cout << "Price: " << library[i].price << endl << endl;
}

Hope this helps !

This question has already been answered. Start a new discussion instead.