compare a string for a specific char

I am trying to convert a user input string that is supposed to a fraction into a double but it refuses to recognize the / char as a char.
Here is the function I am using to do this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  string b ="/";
void dec(string in3)
{
	for(int i=0;i<=in3.length();i++)
	{
		string a =in3.substr(i,i);
		if(a.strcmp(b))
		{
			pos=i;
		}

		
	}
	string top2;
		top2=in3.substr(pos-1);
		
		
		string bottom2=in3.substr(pos+1, in3.length());
	r=(convert(top2))/(convert(bottom2));
}

and the convert function (basically like parseInt to most other languages).
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
int convert(string in)
{
	int out = 0;
	for(int loop=1; loop<=in.length();loop++)
	{
		out *= 10;
		if(in.substr(loop)=="1")
		{
			out+=1;	
		}
		if(in.substr(loop)=="2")
		{
			out+=2;
		}
		if(in.substr(loop)=="3")
		{
			out+=3;
		}
		if(in.substr(loop)=="4")
		{
			out+=4;
		}
		if(in.substr(loop)=="5")
		{
			out+=5;
		}
		if(in.substr(loop)=="6")
		{
			out+=6;
		}
		if(in.substr(loop)=="7")
		{
			out+=7;
		}
		if(in.substr(loop)=="8")
		{
			out+=8;
		}
		if(in.substr(loop)=="9")
		{
			out+=9;
		}
	}
	return out;
}

am I doing something wrong?
I would first make sure that you know what the parameters to substr really mean:
http://www.cplusplus.com/reference/string/string/substr/

If you're looking for a specific character, why not just use find?
http://www.cplusplus.com/reference/string/string/find/
ok so I used find now (and replaced "/" with const char a =47) now it doesn't throw the exception. it just exits the program with a return value 255.
here is the modified code:
1
2
3
4
5
6
7
8
9
10
11
void dec(string in3)
{
	
	pos=in3.find(a);
	string top2;
		top2=in3.substr(0,pos-1);
		
		
		string bottom2=in3.substr(pos+1);
	r=(convert(top2))/(convert(bottom2));
}
Last edited on
Topic archived. No new replies allowed.