I think my "if" statement is broken

Pages: 12
Hello, I am very new to C++ and have a question about my code. The code is suposed to find the index location in a list of 20 user supplied numbers and return in which indexs it finds the supplied number.I believe the problem is in my "if" statement in the main function, but I maybe wrong.Any help would be appreciated.
Here is the code:

#include <iostream>

using namespace std;

int num_list [20];


// my_search takes in a search space, the size of that space, and something to look for
int my_search(int num_list [], int size, int find)
{
//loop through the num_list
for (int x = 0; x < 20; x++)
{
//if thing we are looking for is found
if (find == num_list [x])
{
// notify the caller of a success
return x;
}
} // no else because it would not check entire list before returning false
//here, we've looked through the entire array but could not find what we wanted
return -1;
}



int main ()

{
int find;
int x = 0;

cout << "Please provide 20 integer numbers" << endl;
for (int x = 0; x < 20; x++)
{
cout << "Enter number " << x << ":" << endl;
cin >> num_list [x];
}
cout << "What number are you searching for?" << endl;
cin >> find;

for (int x = 0; x <20; x++)
{
if (my_search(num_list, x < 20, find))
{
cout << "Element found at index " << x << endl;
}
else
{
cout << "Element not found." << endl;
}
}



}


if (my_search(num_list, x < 20, find))
'if' statements don't work like this. To have a function called for each 'x' use a for loop (like u have) and only pass in 1 'x' at a time.

if (my_search(num_list, x, find)) tada !

Also, u normally want to have a clear comparison in ur 'if' statement so that ppl (and u) know what is being tested. Based on ur function u would want this instead:

if (my_search(num_list, x, find) > -1)
Last edited on
soranz thank you for your help. I made the change suggested and I am still having the same problem. I must have more than one. I did a test on the my_search function (note //cout <<x;) when this is not remarked I have found that it prints the find variable then the "Element found at index ", 0 through 19.
ex: find variable 4
output
4Element found at index 0
4Element found at index 1
etc.

here is the revised code, though it is still not working for me. Thank you for the help.

#include <iostream>

using namespace std;

int num_list [20];


// my_search takes in a search space, the size of that space, and something to look for
int my_search(int num_list [], int size, int find)
{
//loop through the num_list
for (int x = 0; x < 20; x++)
{
//if thing we are looking for is found
if (find == num_list [x])
{
// notify the caller of a success
//cout << x;
return x;
}
} // no else because it would not check entire list before returning false
//here, we've looked through the entire array but could not find what we wanted
return -1;
}



int main ()

{
int find;
int x = 0;

cout << "Please provide 20 integer numbers" << endl;
for (int x = 0; x < 20; x++)
{
cout << "Enter number " << x << ":" << endl;
cin >> num_list [x];
}
cout << "What number are you searching for?" << endl;
cin >> find;

for (int x = 0; x <20; x++)
{
if (my_search(num_list , x, find))
{
cout << "Element found at index " << x << endl;
}
else
{
cout << "Element not found." << endl;
}
}



}


See soranz's updated post about how to do the comparison in the if statement.

If you don't want to call my_search 20 times you should not put it inside a loop and instead pass 20 as the second argument.
Peter87 and sorenz, That was a great help. I am almost there. I get the correct response now for where the elements are found but I can not figure out how to only print the "Element not found" when the value is not found in the entire array. right now "Element not found" always prints at the end. Thanks in advance.
New code:


#include <iostream>

using namespace std;

int num_list [20];


// my_search takes in a search space, the size of that space, and something to look for
int my_search(int num_list [], int size, int find)
{
//loop through the num_list
for (int x = 0; x < 20; x++)
{
//if thing we are looking for is found
if (find == num_list [x])
{
// notify the caller of a success
//cout << x;
//return x;
cout << "Element found at index " << x << endl;

}
//return 0;
}

// no else because it would not check entire list before returning false
//here, we've looked through the entire array but could not find what we wanted
return -1;
}



int main ()

{
int find;
int x = 0;

cout << "Please provide 20 integer numbers" << endl;
for (int x = 0; x < 20; x++)
{
cout << "Enter number " << x << ":" << endl;
cin >> num_list [x];
}
cout << "What number are you searching for?" << endl;
cin >> find;

(my_search(num_list , 20, find) > -1);
{
cout << "Element not found." << endl;
}

//if (my_search == -1)



//for (int x = 0; x <20; x++)
//{
//if (my_search(num_list , 20, find) > -1)
//{
// cout << "Element found at index " << x << endl;
//}
//else
//{
//cout << "Element not found." << endl;
//}
//}
}


ps: plz use code tags on the right side under 'Format'

1
2
3
4
5
6
7
8
9
10
11
12
for (int x = 0; x <20; x++)
{
  if (my_search(num_list , x, find) > -1)
  {
     cout << "Element found at index " << x << endl;
     break; //as soon as my_search finds the element, break the for loop
  }
  else
  {
    cout << "Element not found." << endl;
  }
}


I also see that there's a mess of commented and uncommented code in ur last post that wouldn't do what u probably want at this point. Maybe u could post ur final solution ?
Last edited on
soranz, I really appreciate the help. I am posting a cleaned version of the code. I made the changes to suggested and my code still did not work the way it is supposed to. The requirements I need to fullfill are to report back every element that is found or report back that none were found. Right now my code report correctly that it has found the element but it always says it "Element not found" on the last line. Thanks for all your help it has been amazing.

Here is my cleaned up code. Not much different than before just does not have all the remarked out stuff. Also I hope I am using the code tags correctly. I have not posted code before today.

#include <iostream>

using namespace std;

int num_list [20];

int my_search(int num_list [], int size, int find)
{

for (int x = 0; x < 20; x++)
{

        if (find == num_list [x])
        {

            cout << "Element found at index " << x << endl;

        }


}

return -1;
}



int main ()

{
1
2
3
4
5
6
7
8
9
10
11
 int find;
    int x = 0;

    cout << "Please provide 20 integer numbers" << endl;
    for (int x = 0; x < 20; x++)
    {
        cout << "Enter number " << x << ":" << endl;
        cin >> num_list [x];
    }
    cout << "What number are you searching for?" << endl;
    cin >> find;


(my_search(num_list , 20, find) > -1);
{

          cout << "Element not found." << endl;

       }

}


The 1st and 3rd tags are if u want to quote someone. The code tag is the middle one that gives easy to read colors for keywords (blue) and comments (green)...but wrap it around the whole code part ;)

In ur function - it always returns -1...ur first version made more sense because u returned the index if it was found and an error code (ie: -1) if it wasn't found
1
2
3
4
5
6
7
8
for (int x = 0; x < 20; x++)
{
        if (find == num_list [x])
        {
            //this will only print the index it will not store it if u need to use it later...
            cout << "Element found at index " << x << endl;
        }
}


1
2
3
4
5
6
7
8
9
10
//there's no control statement here so the outside () isn't needed/doesnt affect anything...
(my_search(num_list , 20, find) > -1); //the ; ends this statement so >-1 makes no sense...




//ie: the { and my_search are not connected
{   //the stuff in here is always executed because there is no control statement...
          cout << "Element not found." << endl;
 }
Last edited on
You want to use the return value of the function more than once so you should store it in a variable.
int index = my_search(num_list, 20, find);

If index is not negative you print the index, otherwise you print the error message.
1
2
3
4
5
6
7
8
if (index >= 0)
{
	cout << "Element found at index " << index << endl;
}
else
{
	cout << "Element not found." << endl;
}
Last edited on
I think I almost go it last night. I just need to figure out where the return -1; goes. The code finds all of the elements just fine, it just wants to output one line of "Element not found" at the end. Here is the current code. Thanks for your help

#include <iostream>

using namespace std;

    int num_list [20];


    // my_search takes in a search space, the size of that space, and something to look for
    int my_search(int num_list [], int size, int find)
{
    //loop through the num_list
    for (int x = 0; x < 20; x++)
    {
        //if thing we are looking for is found
        if (find == num_list [x])
        {
            // notify the caller of a success
            //cout << x;
            //return x;
            cout << "Element found at index " << x << endl;
        }

    }
    return -1;
        // no else because it would not check entire list before returning false
    //here, we've looked through the entire array but could not find what we wanted
}



int main ()

{
    int find;
    int x = 0;

    cout << "Please provide 20 integer numbers" << endl;
    for (int x = 0; x < 20; x++)
    {
        cout << "Enter number " << x << ":" << endl;
        cin >> num_list [x];
    }
    cout << "What number are you searching for?" << endl;
    cin >> find;

        if (my_search(num_list, 20, find) == -1)
            {
               cout << "Element not found" << endl;
            }

}

Because you commented out return x in your function, so it will always return -1. Therefore, every time you search, regardless of if the value exists in the array or not, it will return -1 and you'll display "Element not found"
When I commented
return x;
back in it did not print anything, so I moved it to after the
cout << "Element found at index " << x << endl;
but that would only find one occurance of the element instead of all of them. I replaced the
return -1;
with
return x;
(I like to try things to see what will happen) and the compiler complained. As of right now I put it back the way it was in my last post so I have a consistant place to start from.
Well you have a few issues here, you can't return more than one value, not that you're ready for anyways, and you can print out each occurance of a certain value, but why are you trying to return values then? Do you just want your function to display the index? Do you want to return the value of the first index, starting at a given point and print the index out in main? There is a lot of questions that need answered yet.
An example would be, out of the 20 numbers two numbers are 4's. One at element 4 and one at element 14. So I would print
Element found at index 4
Element found at index 14

Then the program ends. If none of the 20 numbers where the one that I was searching for then I need to output
Element not found
There are easily 4 different ways to do this, but I feel the easiest would be to make your function void since you won't need to return anything anymore, and create a boolean variable to see whether or not a value was found in the array. After the for loop in your function, you can check to see if that variable is true or false. If it's false, it means it didn't find your "find" variable, and you can print out "Element not found"
I have tried that twice with no success. I am required to return a -1 also.
What are your specific requirements? What else does the function have to return? Possibly the index of each search? Does it specify that it must return multiple elements as well?
Here is the actual requirements:
Prompt the user for twenty integers. Then prompt the user for one integer to look for, x. If x is contained within the twenty list of integers output all the subscripts where it was found. It the element wasn't found, let the user know.

The output would look just report the indexs where the number was found. I must report every occurance.
Element found at index 2
Element found at index 5
Element found at index 19

etc.

If it is not found the report back
Element not found
Where does it say that you need to return -1?
The return -1 came from my instructor when I asked him for help. He had not shown that in his examples but insists that I do it.
Pages: 12