Trouble with recursion

Hey all, I'm kind of in a predicament here. My C++ class was canceled because the teacher was sick, and the school server is now down so I can't access the lecture material, so I'm pretty confused on this recursion assignment.
We have to take a function and have it operate the same way using recursion, we have to do this for two classes, each are slightly different.

Class 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Integer::isNaN(string s)
	{
		string::iterator p;
		for (p = s.begin(); p < s.end(); p++)
		{
			if (!isdigit(*p) && *p != '.')
			{
				return true;
			}
		}
		return false;

	}


Class 2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool Double::isNaN(string s)
	{
		int pos;
		pos = s.find(".", 0);
		if (pos != string::npos)
		{
			pos = s.find(".", pos + 1);
			if (pos != string::npos)
			{
				return true;
			}
		}

		string::iterator p;
		for (p = s.begin(); p < s.end(); p++)
		{
			if (!isdigit(*p) && *p != '.')
			{
				return true;
			}
		}
		return false;
	}

If anyone can point me in the right step that would be fantastic!
Last edited on
For recursion you need to replace the for loop with a call to the function itself.

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool Double::is_first_dot(string::const_iterator it, string::const_iterator end_it)
	{

if(it != end_it)
{
  if(*it != '.')
    return is_first_dot(it + 1, end_it); // Note: + 1
  else
     return true;
}
else
  return false;
	}
This is how to search for the first dot recursively.

By the way: NaN stands for Not a Number. Hence it should return true if it is actually not a number.
Topic archived. No new replies allowed.