Single error: Quick Fix, needed.

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
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
        //declarations
        ifstream file1;
        ifstream file2;
        ofstream file3;
        string line;
        string word;
        string array[21];
        int loop=0;

        file1.open("text1.txt");
        file2.open("text2.txt");
        getline (file1,line, '\0');
        getline (file2,word, '\0');
        file1.close();
        file2.close();
        array[loop] = line + word;
        sort(array[]);
        file3.open("text3.txt");
        file3 << array[loop];
        file3.close();
}

void sort(float array[])
{
 int i, j;
 float temp = 0;
 for (i=0; i<21; i++)
  {for(j=(i+1); j<22; j++)
    {if (array[i] > array[j])
        { temp = array[i];
          array[i] = array[j];
          array[j] = temp;
        }
    }
  }
}


I keep getting only a single error,

file.cpp: In function ‘int main()’:
file.cpp:34: error: expected primary-expression before ‘]’ token
sort(array[]);

array is an array name. If you have an array name followed by [brackets], that means you are indexing the array. Here, you do not give an index (your brackets are empty)... so the compiler is complaining.

Since you do not want to pass a single element to the array, but rather want to pass a pointer to the entire array, you do not need brackets at all. Simply do this:

sort(array);
Did that, new error.

file.cpp: In function ‘int main()’:
file.cpp:34: error: no matching function for call to ‘sort(std::string [21])’



Thanks for the quick reply, by the way!

EDIT: I've been fiddling around with this for a couple hours and I noticed the algorithm was still there so I decided to remove it and compile it.

Got a new error,
file.cpp: In function ‘int main()’:
file.cpp:33: error: ‘sort’ was not declared in this scope
Last edited on
@PrimeTimeBio

Few things.
First, you need to declare the function above int main(), if the function is located after main.
Second, you declared array[21] as a string, but are calling it in the function as a float.
Third, if you're trying to write into array[], you probably need to increase loop with a while(loop<21);, or a for loop.
What @whitenite1 said.
Also, you have an array of 21 elements (0 - 20), but you're trying to read off the end of the array with
for(j=(i+1); j<22; j++)
@PrimeTimeBio

Here is a bubble sort loop, which is what I think you were trying to accomplish in your code. Hope it helps out.

1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0; i < 21; i++)
{
	for(int j = 0; j < 20 - i; j++)
	{
		if(array[j] > array[j+1])
		{
			int temp = array[j];
			array[j] = array[j+1];
			array[j+1] = temp;
		}
	}
}
Topic archived. No new replies allowed.