Calling function from inside loop

Hi Everyone,
For full disclosure this is a homework assignment. I pretty much have it figured out, but have something wrong with the way my loop inside main calls and works with bool value not_dup output by searchArray. Also getting a compiler error (running Xcode GNU gCC compiler) stating my function searchArray is referenced from main. If anyone could take a quick look and just point me to the one or two word or character mistake that will get me back on track I would really appreciate it! My objective is to get an integer from the user, check to see if it is a duplicate, and if not, copy it into array d_array. Thanks again.

#include <iostream>
using namespace std;

//Function prototype
bool searchArray(int[], int, int);


int main ()
{
const int AR_LEN = 11;
int d_array[AR_LEN], count, dist_num = 1, test_num;
bool not_dup;

cout << "Please enter ten integers, hitting return after each one: \n";

for (count = 0; count < (AR_LEN - 1); count++)
{
cin >> test_num;
searchArray(d_array, AR_LEN, test_num);
if (not_dup)
{
d_array[count] = test_num;
dist_num += count;
}

}

cout << "You entered " << dist_num << " distinct numbers: ";
for (count = 0; count < (AR_LEN - 1); count++)
cout << d_array[count] << " ";

cout << endl;



return 0;
}

//****************************************************************
//Definition of function search_array. *
//This function examines the stored values in d_array for *
//duplicates and returns the bool variable not_dup accordingly. *
//****************************************************************

bool search_array(int d_array[], int AR_LEN, int test_num)
{
int index = 0;
bool not_dup = true;

while (index < (AR_LEN - 1) && !not_dup)
{
if (d_array[index] == test_num)
{
not_dup = false;
}
index++;
}
return not_dup;
}

Your search_array() doesn't output to the not_dup in your main().

EDIT: What firedraco said just below me is also quite critical, and in fact that should generate a linker error.

-Albatros
Last edited on
The error is that your prototype's name and when you call it, it is called "searchArray", but when you define it (at the bottom), it is called "search_array".
Albatross and firedraco,
Thank you both very much for your replies--it's always those simple details that slide past tired eyes after hours at the screen. I am now stuck on some logic issues. It appears my searchArray function is not outputting a false value and so d_array is being populated even if the new value test_num is a duplicate. I'm also unsure as to whether I understand the logic of the user input properly to begin with: I am trying to get it to where each entry followed by a hit of the return key will be passed to function searchArray and only upon getting a true bool value of not_dup will d_array get populated and given the subscript of whatever number (dist_num) the program has progressed through. Thanks again everyone, it really helps to get those little pointers that give the aha moment! Also, I made some changes to the previous code:

int main ()
{
const int AR_LEN = 10;
int d_array[AR_LEN], count, dist_num = 0, test_num;
bool not_dup;

cout << "Please enter ten integers, hitting return after each one: \n";

for (count = 0; count < AR_LEN; count++)
{
cin >> test_num;
not_dup = searchArray(d_array, dist_num, test_num);
if (not_dup)
{
d_array[dist_num] = test_num;
dist_num += 1;
}
}

cout << "You entered " << dist_num << " distinct numbers: ";
for (count = 0; count < dist_num; count++)
cout << d_array[count] << " ";

cout << endl;

return 0;
}

//****************************************************************
//Definition of function searchArray. *
//This function examines the stored values in d_array for *
//duplicates and returns the bool variable not_dup accordingly.
*
//****************************************************************

bool searchArray(int d_array[], int dist_num, int test_num)
{
int index = 0;
bool not_dup = true;

while (index < (dist_num + 1) && !not_dup)
{
if (d_array[index] == test_num)
{
not_dup = false;
}
index++;
}
return not_dup;
}
searchArray is supposed to examine the array for duplicate numbers, right?

Use [code] tags, if you would please, first and foremost.

Second, it seems that you can use a for loop here. If you have a duplicate, set not_dup to false. Why a for loop? Because it's easier on the eyes.

For loop condition: not_dup is true and your index is less than dist_num.

Your function never returns false because of this in your while loop:
&& !not_dup


-Albatross
Thanks Albatross,
And sorry for the lack of code tags, this is new to me (tried to preview my post and saw it was messy, and did see those buttons on the right, but didn't know what to do with them). Really appreciate the advice, I'll see what I can figure out.
Hi Albatross,
Sweet victory at last! My general logic was on the right track, but just missed those few things--thank you so much for your help! This semester has been my first exposure to programming and though frustrating as hell sometimes, I am starting to find it to be quite a fascinating subject. Thank you very much for the little cyber nudge to get this infant programmer on his way! Enjoy your evening.
Topic archived. No new replies allowed.