Incorrect Array Assignment Error

I keep getting the error:
incorrect array assignment
at line #14. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void testStrings( char fileName [] ) {
    ifstream infile;
    ofstream outfile;
    char str1 [51];
    char str2 [51];
    char bigArray [1001] [51];

    infile.open (fileName);
    for (int i=1; infile.peek() != EOF; i++ ) {
        infile >> str1;
        while (infile.peek() != EOF){
            infile >> str2;
            if (strcmp (str1, str2) < 0){
                bigArray [i] = str1;
                cout << "\n 1.  " << bigArray['n'];
                //strcpy(str1[n],tempBuffer);
            }
        }
    }
}
ISO C++ forbids assignment of arrays. Copy the chars across.
Last edited on
a good suggestion is to use strings instead of char arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void testStrings( string fileName ) {
    ifstream infile;
    ofstream outfile;
    string str1;
    string str2;
    string bigArray [1001];

    infile.open (fileName.c_str());
    for (int i=1; infile.peek() != EOF; i++ ) {
        infile >> str1;
        while (infile.peek() != EOF){
            infile >> str2;
            if ( str1 < str2 ){
                bigArray [i] = str1;
                cout << "\n 1.  " << bigArray['n'];
            }
        }
    }
}



On top of solving your current problem and also being easier to use... there is no chance of buffer overflow and memory corruption if a user enters a string longer than 50 characters.
Last edited on
:)

I am not allowed to use strings, only arrays of characters. Since everyone keeps suggesting using string, I'm going to do it anyway. Maybe the instructor won't notice.

But if there's a good way to do this with a char array, I'm listening!
Shoot, now I get the error:


error: no match for 'operator>>' in 'infile >> str2'|


I've progressed from the wrong syntax for a char array to the wrong syntax for a string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void testStrings( char fileName [] ) {
    ifstream infile;
    ofstream outfile;
    string str1 [51];
    string str2 [51];
    string bigArray [1001] [51];

    infile.open (fileName);
    for (int i=1; infile.peek() != EOF; i++ ) {
        infile >> str1; //string to compare against
        while (infile.peek() != EOF){
            infile >> str2; // compares each word against str1
            if (strcmp (str1, str2) < 0){
                bigArray ['i'] = str1;
                cout << "\n 1.  " << bigArray['i'];
                //strcpy(str1[n],tempBuffer);
            }
        }
    }
}

But if there's a good way to do this with a char array, I'm listening!


Copy the chars (if that's what you're actually trying to do - not much of this code makes sense).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void testStrings( char fileName [] ) {
    ifstream infile;
    ofstream outfile;
    char str1 [51];
    char str2 [51];
    char bigArray [1001] [51];

    infile.open (fileName);
    for (int i=1; infile.peek() != EOF; i++ ) {
        infile >> str1;
        while (infile.peek() != EOF){
            infile >> str2;
            if (strcmp (str1, str2) < 0){
		  for (int j=0; j< 51; ++j)
		   { bigArray [i][j] = str1[j];}
                cout << "\n 1.  " << bigArray['n'];
                //strcpy(str1[n],tempBuffer);
            }
        }
    }
}



What are you trying to do where you put the index value in apostrophes (e.g. bigArray['n'] instead of bigArray[n]) ?
Last edited on
I tried it both ways ['i'] and [i]. I was just trying to get rid of the incorrect array assignment error.

Oh, I see how this could work counting through i and j. I'll give this a shot with char arrays again.
Well that did the trick! Thanks! Now my problem has to do with nested EOF statements. By the time the first loop gets a chance to look at the file a second time, the second loop has apparently already taken it to the end of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void testStrings( char fileName [] ) {
    ifstream infile;
    ofstream outfile;
    char str1 [51];
    char str2 [51];
    char bigArray [1001] [51];

    infile.open (fileName);
    for (int i=1; infile.peek() != EOF; i++ ) {
        infile >> str1; //string to compare against
        do {
            infile >> str2; // gets compared to str1
            if (strcmp (str1, str2) < 0){
                for ( int j=0; j<51; j++) {
                    bigArray [i][j] = str1[j];
                }
                //strcpy(str1[n],tempBuffer);
            }
        } while (infile.peek() != EOF);
        cout << "\n 1.  " << bigArray[i];
    }
}



           **************************
           * FILE READ/COPY/ANALYZE *
           **************************

This program will read in a file, copy it
and create a file containing data about the file.

Enter a file name to be displayed and copied: test.txt
Okay: Copying file: test.txt to new file: Copy of test.txt
Reading: I'll have a Sam Adams.
Reading: It's 9:30 in the morning.
Reading: Come to think of it, I'll have a Sam Adams also.
There were 3 lines read

Copying: I'll have a Sam Adams.
Copying: It's 9:30 in the morning.
Copying: Come to think of it, I'll have a Sam Adams also.
There were 3 lines copied

Reading: I'll have a Sam Adams.
Reading: It's 9:30 in the morning.
Reading: Come to think of it, I'll have a Sam Adams also.
There were 3 lines read

Number of words contained in file: 21

 1.  I'll
Process returned 0 (0x0)   execution time : 1.964 s
Press any key to continue.



I'm trying to work around the my wonky test for EOF in the for loop, but it's still not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void testStrings( char fileName [] ) {
    ifstream infile;
    ofstream outfile;
    char str1 [51];
    char str2 [51];
    char bigArray [1001] [51];
    int count = 0;
    int i = 0;
    int j = 0;

    infile.open (fileName);
    for (i=0; infile.peek() != EOF; i++ )
    {
        infile.open (fileName);
        infile >> str1; //string to compare against
        if (count == i)
        {
            while (infile.peek() != EOF)
            {
                infile >> str2; // gets compared to str1
                if (strcmp (str1, str2) < 0)
                {
                    for ( int j=0; j<51; j++)
                    {
                        bigArray [i][j] = str1[j];
                    }
                }
            }
            cout << "\n 1.  " << bigArray[i];
            infile.close ();
            count ++;
        }
    }
}
Topic archived. No new replies allowed.