-2

build log message:

enter image description here

this is how the code looks like:

#include <iostream>

using namespace std;
int lsearch(int[],int,int);

int main()

 {


int N,ITEM,INDEX,ar[20];

cout<<"how many elements?(max 20)\n"<< endl;

cin>>N ;

cout<<"\n enter elements\n";

for(int i=0;i<N;i++)

cin>>ar[i];

cout<<"your array is as follows :\n";

for (int i =0;i<N ;i++ )


cout<<ar[i] <<"\n";

cout<<"enter element to be searched for:\n";

cin>>ITEM;

INDEX=lsearch(ar[ ],N,ITEM);

if(INDEX==-1)

cout<<"element not found";

else

cout<< "item found at index:"<<INDEX<<"position:"<<INDEX+1;

return 0;
}

int lsearch (int ar[ ],int N,int ITEM)
{

    for (int i=0;i<N ;i++ )
{
    if  ( ar [i]==ITEM)

    return i;
}

        return -1;
}

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

thanks in advance :)

raghav
  • 11
  • 1
  • 4
  • 3
    your image does not match the code you posted. Anyhow you should not post imgages of code. Please include the error messgae in the question, it should contain information on the line where the error occurs, and make sure that the code you post acutally does cause that error – 463035818_is_not_an_ai Apr 24 '20 at 12:38
  • 1
    I closed as a typo. Also the code that is now in text does not match the picture or have the bug. – drescherjm Apr 24 '20 at 13:06

1 Answers1

4

You forgot a ; in line 4 of your code dude! (In the image you posted.)

Actually there is no need of line 4: int lsearch(int [], int, int). Because in the next line you are defining the function itself. You can skip the prototype declaration if you wish.

And from next time please post proper code, not just an image. And by code I mean actual code that is causing error. Here the code in your image is different from the one which you have typed in your post!

In your typed code you are calling lsearch as lsearch(arr[], N, ITEM) [line 34]. A call should be made like this: lsearch(arr, N, ITEM).

Here is you corrected code:

#include <iostream>
using namespace std;

int lsearch(int [], int, int);

int main() {
    int N, ITEM, INDEX, ar[20];
    cout << "How many elements? (max 20): " << endl;
    cin >> N;
    cout << "Enter elements: " << endl;
    for (int i = 0; i < N; i++)
        cin >> ar[i];
    cout << "Your array is as follows: " << endl;
    for (int i = 0; i < N; i++)
        cout << ar[i] << endl;
    cout << "Enter the element to be searched for: " << endl;
    cin >> ITEM;
    INDEX = lsearch(ar, N, ITEM);
    if (INDEX == -1)
        cout << "Element not found!" << endl;
    else
        cout << "Item found at index: " << INDEX << " position: " << INDEX + 1 << endl;
    return 0;
}

int lsearch(int ar[], int N, int ITEM) {
    for (int i = 0; i < N; i++)
        if (ar[i] == ITEM)
            return i;
    return -1;
}

Sample Run:

How many elements? (max 20): 5
Enter elements: 1 2 3 4 5
Your array is as follows: 
1
2
3
4
5
Enter the element to be searched for: 4
Item found at index: 3 position: 4

This code is practically the same (I guessed this code from your image):

#include <iostream>
using namespace std;

int lsearch(int[], int, int); // Your line 4 which isn't necessary and where you missed a semi-colon!
int lsearch(int ar[], int N, int ITEM) {
    for (int i = 0; i < N; i++)
        if (ar[i] == ITEM)
            return i;
    return -1;
}

int main() {
    // same as in above code
}

You should also check out this thread on why "using namespace std" is considered a bad practice.

brc-dd
  • 10,788
  • 3
  • 47
  • 67