Returning from function

I am trying to return from a function when it figures out there is nothing in a list. What is the best way to do this??

Here is my function
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
string Circlist::select(int n) 
{string result;
Node* p;
Node* temp, *prev;
bool finished = false;
if (start  -> s == "\0")
	{
	cerr << "** Cannot select from empty list";
	return 0;
	}
else if (start -> link == start)
{	result = start -> s;
	delete start;
	start = NULL;
	return result;
}
else
{
p = start -> link;
int loop = 1;
while (!finished)
	{while (loop < n + 1)
 		{prev = p;
  		 p = p -> link;
  		 loop++;
 		}
	 loop = 1;
	 if (start -> link == start)
	 	{result = start -> s;
	 	delete p;
	 	start = NULL;
		}
	else if (p == start)
		{temp = p -> link;
		 cout << p -> s << endl;
		 delete p;
		 p = prev;
		 prev -> link = temp;
		 start = prev;
	 	}
	else
		{temp = p -> link;
		cout << p -> s;
		delete p;
		p = prev;
		prev -> link = temp;
		}

	}
return result;
}
}


My program crashes in this function.
For future readers and reinforcemnet of my own long term memory this program crashed because it returned 0 on line 9 when infact it should return a string.
*function. Is my thinking correct. It was called like this assert (c.select(5).length() == 0); // test of error handling

if it was called differently would the program still crash with return 0? How do i exit the function without returning anything but continuing my main() function? Is it due to the fact that assert was used with the condition it crashed?

what is the correct term for a term in brackets i.e if (x==y)? cos im using condition but is obvs wrong...

Sorry for lots of questions.
What does return 0 actually mean?
Returning zero (= NULL) when your return type is std::string triggers the std::string(const char*) version of the constructor, and std::string doesn't like being fed NULL pointers :-(

You can return ""; as well as resturn std::string();
i see i see. Thanks.
Last edited on
Topic archived. No new replies allowed.