0

The function called skipped over when i run the program. What's the problem?

#include <iostream>
using namespace std;

void searchId(int id[], int& idNum);

int main()
{
    int idNum;
    int count;
    int *id;
    int Numid;

    cout << "Please input the number of id numbers to be read "<<endl; //
    cin >> idNum;

    id = new int[idNum]; //To allocate the float variable sales here

    for (count = 0; count < idNum; count++)
    {    
        cout << "Please enter an id number " << endl;
        cin >> id[count];
        id[count]=Numid;
    }    

    cout << "Please input an id number to be searched " << endl;
    cin >> nid;

    searchId(Numid,idNum);//Calling function

    delete id;

    system("pause");
    return 0;
}

void searchId(int Numid,int idNum)
{
    int count;
    int nid;

    for (count = 0; count < idNum; count++)
    {
        if(nid == Numid)
        cout << nid <<"is in the array "<< endl;

        else
        cout << nid  <<"is not in the array "<< endl;
    }

}
2
Contributors
1
Reply
10
Views
4 Years
Discussion Span
Last Post by Labdabeta
0

It isn't skipping it... I'll explain: When you created searchId you defined it as returning void, and taking an integer array (id[]) and a reference to an integer (idNum). Then you told the compiler that right now the function does nothing (that ; actually kinda means {}... kinda). Anyways, normally you would later define what this function does and the compiler would fill it in. You probably think you did that because you recreated searchID and defined what it did. The issue is that IT WASN'T THE SAME! Basically, you can pretend that the compiler called your first function void_searchId_int[]_int& (incorporating its types) and the second one would be called void_searchId_int_int... note that these are not the same! What you need to do is ensure that they match, or define the first one. You have another issue as well, you aren't giving enough info to your searchId function. It will of necessity require an integer array (the IDs to search) as well as the size of said array (to tell you how long to search for) and the value to search. It is the middle one that you are missing. Here is the pseudo-c++ code for what you want to do:

void searchId(int ids[], int idLength, int val);
int main()
{
    //you actually got this part pretty much correct as far as I can tell
}
void searchId(int ids[], int idLength, int val)//note that the NAMES of these don't matter, but the TYPES definately do!
{
    for each element in ids (note that you need to search from 0->idLength):
    {
        if the element is equal to val
        {
            output that val is in the array
            return;//to get out! we found the value, we are done!
        //DO NOT PUT THE ELSE HERE... IT SHOULD GO OUTSIDE THE LOOP!!! (WALK THROUGH THE FUNCTION IN YOUR HEAD TO SEE WHY!)
    }
    output that the val is not in the array
}

Note that this is still probably NOT the best way to do this. Usually you want functions to act independently, IE: not use global variables like cin/cout! Basically you should instead have searchId return a bool value (which is 1 or 0) 1 if the value is in the array, 0 if it isnt. Then you can say something like this in main:

if (searchId(myArray,myArraySize,myValue))
    cout<<"The value was found :)\n";
else
    cout<<"The value was not found :(\n";

This will make your code much cleaner and easier to read and debug.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.