function error

Hello. I get an error in lines 5 and 33. I have forgotten much about functions, errors say :
1.'i' was not declared in this scope
2. expected ')' before ',' token
3. expected unqualified-id before 'int'


My function should find the highest number in an array. I am using my function in line 15
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
  #include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
void funkcija(int p[], int k, int & k);
int main()
{
    int n, b = 0,k = 0;
    int p[100], sum = 0, i= 0;
    ifstream fd ("duota.txt");
    ofstream fr ("rezultatai.txt");
    fd >> n;
    for (int i = 0; i < n ; i++){
        fd >> p[i];
    }
        funkcija(p[],k);

    for ( int i = 0; i < n ; i++){
        if (p[i] == k){
            fr << i+1 << "  ";
        }
        else if (k == 0){
        fr << "nera";
        }
    }
    if (sum > 0){
        fr << fixed << setprecision(0) << sum / (n - b) << endl;
    }
    else {
        fr << "nera" << endl;
    }
    return 0;

}
void funkcija(int p[], int k,, int & k){
 for (int i = 0; i < n; i++){
        if (p[i] > k){
             k = p[i];
        }
}
}
Last edited on
void funkcija(int p[i], int k, int i, int & k);

That's just not how to declare a function. Each parameter must specify the type of the parameter, and may specify the name of the parameter.

If the first parameter is meant to be an int array, named p:

void funkcija(int p[], int k, int i, int & k);


While I'm here, it's pretty safe to say that you should use an array when you have to and a vector (or std::array) in all other situations. In this case, use a vector.

1
2
3
   for (int i = 0; i < n ; i++)
        fd >> p[i];
        funkcija(p[i],k, i,  k);


Are both those lines meant to be inside the for loop? They're not. Only the first one is. Use braces for every for loop, always.If you don't, one day you'll get it wrong or someone else who has to maintain or understand your code will.


1
2
3
4
5
6
void funkcija(int p[i], int k, int i, int & k){
 for (int i = 0; i < n; i++){
        if (p[i] > k){
             k = p[i];
        }
}

Your function passes in a variable i, and then you never use it; you create a whole new one called i inside the function. What's the point?

Edit: Please don't change the code in the original post. it makes the thread hard to follow when people's comments are about code that no longer exists.
Last edited on
So I fixed the array thing, deleted the 'i' variable, and only the 1st line was supposed to be in the loop, so i put the brackets there, maybe it is clearer now.

The thing is that the variable 'k' is repetetive. I use it in the start of the function and also I get the new value of 'k' as a result, so I get errors:

1.(in line 5 and 33) conflicting declaration 'int& k'|
2.(in line 16)invalid conversion from 'int' to 'int*' [-fpermissive]|

Last edited on
void funkcija(int p[], int k, int i, int & k)

Inside this function, when you use the variable k , do you mean the second parameter or the fourth parameter? How is the compiler meant to know which you mean?

Do you understand why this code makes no sense:
1
2
3
int a;
float a;
cout << a; // WHICH a is being output? 

That's the mistake you're making. Two variables with the same name.
Thank you, I deleted the first 'k' and changed it to 0 zero instead.

However, I still get an error in line 16, it is expecting primary expression before ']'
What makes your code hard to manage is the first part:
1
2
3
4
int main()
{
    int n, b = 0,k = 0;
    int p[100], sum = 0, i= 0;


You declare all the variable at the beginning of the function, so that you make it hard to remember which one you used and for which purpose. I strongly suggest you to avoid that bad habit. Declare variables only where you really need them.

For example: where do you use ‘b’?
Please, have a look at the following code. It compiles but perhaps doesn’t do what you’d like it to do:
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
45
46
47
48
49
50
51
52
53
54
#include <fstream>
#include <iomanip>
#include <iostream>

// void funkcija(int p[], int k, int& k); // <-- two arguments with the same name
void funkcija(int p[], int n, int& k);

int main()
{
    // read from file
    std::ifstream fd ("duota.txt");
    int n {};
    fd >> n;
    int p[100] {};
    for (int i = 0; i < n ; i++){
        fd >> p[i];
    }
    fd.close();
    
    int k = 0;
    funkcija(p, n, k); // k will be modified by the function

    // write to file
    std::ofstream fr ("rezultatai.txt");
    for (int i = 0; i < n ; i++) {
        if (p[i] == k){
            fr << i+1 << "  ";
        }
        else if (k == 0) {
            fr << "nera";
        }
    }
    int sum = 0,
        b   = 0;
    if (sum > 0) {
        // integer division: it returns an integer
        fr << std::fixed << std::setprecision(0) << sum / (n - b) << '\n';
    }
    else {
        fr << "nera\n";
    }
    fr.close();
    return 0;
}

// void funkcija(int p[], int k,, int & k) <-- double comma
void funkcija(int p[], int n, int& k)
{
    for (int i = 0; i < n; i++) {
        if (p[i] > k) {
            k = p[i];
        }
    }
}


duota.txt:
5
11
12
13
14
15


rezultatai.txt:
5 nera

Last edited on
Topic archived. No new replies allowed.