2

I am trying to make a program in C++ that will search for a desired value in an array of size 10 using a separate search function. Below is the code:

main.cpp

#include <iostream>   
#include <array>   
using namespace std;

int main()  
{
    cout << "Welcome to the array linked list program.";

    int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    int d = 0;
    cin >> d;
    while (d =! 0)
    {
        cout << "Number to be found";
        cin >> d;
        bool found = seqSearch1(sanadA, 10, d, -1);
        cout << found;
    }
}

seqSearch1.cpp

#include <iostream>
using namespace std;

bool jw_search (int *list, int size, int key, int*& rec)
{ //Basic sequential search.
    bool found = false;
    int i;

    for(i=0;i<size;i++)
    {
        if (key == list[i])
        {
            break;
        }
        if (i < size)
        {
            found = true;
            rec = &list[i];
        }
    }
    return found;
}

I get the errors:

C:\Users\tevin\Documents\sanad\main.cpp|13|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|

C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|

C:\Users\tevin\Documents\sanad\main.cpp|19|error: 'seqSearch1' was not declared in this scope|

I need help figuring why this happens.

2 Answers2

3

I assume that the error occurs on this line:

bool found = seqSearch1(sanadA, 10, d, -1);

The problem is that you have not declared any function named seqSearch1(). Instead you have a function named jw_search(). So you can change the line to this:

bool found = jw_search(sanadA, 10, d, -1);

But you also need a header file called seqSearch1.h with the following line:

bool jw_search (int *list, int size, int key, int*& rec);

And finally add this line to the top of main.cpp:

#include "seqSearch1.h"

When you compile your code, you will need to include all source files in the command. For example, if you are using g++, you can do something like this:

g++ main.cpp seqSearch1.cpp

To understand how this works, you need to learn about header files and the difference between a function declaration and a function definition. You should also learn about the difference between the compiler and the linker.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • @LAD "but the code doesn't show that anywhere" The code doesn't show what? – Code-Apprentice Aug 13 '18 at 01:45
  • @LAD As you say, the OP clearly marked a section of code as `seqSearch1.cpp`. They don't clearly show the command used to compile, though, so I've added more details about how to do that correctly. And there are several other issues with the code, but I'm not going to take the time to dig into it that much. – Code-Apprentice Aug 13 '18 at 01:50
1

Code-Apprentice has the direct answer to your question. If you want the code in multiple files then a declaration of the seqSearch1 function will need to be main.cpp or included via #include directive

The code has multiple problems. I've fixed it up a bit for you and put it in a single file.

#include <iostream>

#include <array>
using namespace std;

bool seqSearch1 (int *list, int size, int key, int& rec)
{//Basic sequential search.
bool found = false;

int i;

for(i=0;i<size;i++)
{
    if (key == list[i])
    {
        found = true;
        rec = i;
        break;
    }
}
return found;
}

int main()
{
     cout << "Welcome to the array linked list program." << endl;

int sanadA[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int d = -1;
while (d != 0)
{
    cout << "Number to be found, 0 to end?";
    cin >> d;
    if(d == 0) break;
    int index = -1;
    bool found = seqSearch1(sanadA, 10, d, index);
    if(found) cout << "Found" << endl;
    else  cout << "Not Found" << endl;
}
}

Several issues:

  1. The function was referred to by the wrong name.
  2. The loop structure was a confused.
  3. The fourth argument to seqSearch1 had type confusion.
Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23