hey guys, this is what i have so far...

this part is from the main.cpp

double x;  
        cout<<"Enter a number to search for:  \n";
        cin>>x;
        list.search(x);

        
        if (list.search(x))
            cout << "\n" << x << " IS on the list!  AT POSITION  :" <<endl;
        else
            cout << "\n" << x << " IS NOT on the list." << endl;

and this part is from the .h header

int floatlist::search(double x)
{
    
    listnode *p = head;
    while (p != NULL)
    {
        if (p->value == x)
            return 1;
        else
            p = p->next;     
             
    }
    return 0;

so far it works, im able to enter some numbers and have the search function tell if the number is in the list or not. the only thing i cant figure out is how to say where the position of the number being found is.

i was thinking of putting a counter++; in the while(p != NULL) part, so everytime it goes through the numbers, it counts. and then return the counter value. i couldnt get that to work though.

any help would be appreciated.
-j

If you can return an iterator, but I think that would be out of your scope. So
instead return a index counter, although I am not sure how good thats gonna do you.
Try something like this :

int floatlist::search(double x)
{
    int index = -1;
    listnode *p = head;
    while (p != NULL)
    {
        ++index;
        if (p->value == x)
               break;
        else
            p = p->next;     
             
    }
    return index;

Then you can check your code, if the search method returns -1, then its not in the
list else it is.

hmm i couldnt get it working right. when i tried this, index output a value of 376 every time :S

im only suppose to return one value, the double x in the search function.

I think the problem is in the main function .... I notice from the code that you have posted in post 1 you are calling search twice... Can you post an updated version of your main function.

i actually got it working. this is what i did..

main.cpp

double x;  
        cout<<"Enter a number to search for:  \n";
        cin>>x;
        list.search(x);

search function:

int floatlist::search(double x)
{
    
    int counter;
    
    listnode *p = head;
    while (p != NULL)
    {
        
        counter++;
        if (p->value == x){

cout << "\n " << x << " IS on the list!  AT POSITION  :"<<counter<<endl;

           return 1;
}
        else
            p = p->next;     
             
    }
return 0;

}

thanks for the help guys

This article has been dead for over six months. Start a new discussion instead.