dont know how to return value from funct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[code]Write your question here.

[code]
 #include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
node *next;
};
typedef node *list;
list head;
int search(list, int);
int main()
{
int dat, item;
char ch;
cout<<"Do you wish to enter data??"<<endl;
cin>>ch;
list temp;
int loc;
head=NULL;
while(ch=='y'||ch=='Y')
{
cout<<"Enter data"<<endl;
cin>>dat;
temp=new node;
temp->data=dat;
temp->next=head;
head=temp;
cout<<"Enter more data"<<endl;
cin>>ch;
}
cout<<"Item you wanna search"<<endl;
cin>>item;
loc=search(head, item);
cout<<"Item found at "<<loc<<endl;
return 0;
}

int search(list ptr, int item)
{
int loc=NULL;
while(ptr!=NULL)
{
if(item>ptr->data)
{
ptr=ptr->next;
}
else if(item==ptr->data)
{
loc=ptr;
//return loc;
}[code]
else
{
ptr=NULL;
}
}
return loc;
}

[[/code]/code][/code]
Return what value from what function?

This looks like the same code you posted in another thread.
http://www.cplusplus.com/forum/beginner/137187/

Lines 1,3,55: You can't nest code tags.

As I said in your previous thread:
Line 53: You're trying to assign a pointer (ptr) to an int (loc). Those are different types and that is not a valid assignment.

If you want to return an item's ordinal location within the list, you're going to need to count the number of nodes you traverse and return that count.

Or if you want to return a pointer to the item in the list, then you need to change the return type of your function so that it returns a pointer, not an int.
Last edited on
AbstractionAnon has covered your problem, but as a little side tip, please learn to indent and format your code effectively. It makes it so much easier for yourself and us to understand.
Last edited on
What should be the return type if i want to return the pointer??
What is the type of your pointer?
Hint: You pass it in as an argument at line 42.
Topic archived. No new replies allowed.