why string subscript out of bound exception is coming .??

the program is all about token separation(separatin keywords,identifiers,..
.. frm user i/p code ). plz help me out guyzz i am new to this forum : following is code:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
[code]void main()
{ 
	int i = 0 , j = 0 ;	
	string str ;	                 // str contains i/p string 	
	string str1 = "" , temp = "" ;	
	string ident = "" ;
	string key="";                //storing identifiers
	string consta="";             //storing constants
	string operat="";             //storing operators
	string symb="";               //storing symbols 	
	string str3[]={"int","float","long","double","char","string","boolean","if","else"
		,"for","while","switch","class","include","void","cout","cin"};//array of keywords
	string str4[]={"+","-","*","/","%","<",">","=","!"};//array of operators
	cout<<"Enter a string\t:";
	getline(cin,str,'$');         //getting input	
	cout<<"\n entered string-\t"<<str<<endl;			
	j=str.length();               //getting length of string
	while(j>0)
	{						
		while(j>0)                //inside this block each strings are separated 
		{ 
			if(str[i]==' ')
				break;
			temp=str[i];			
			str1.append(temp);
			i++;
			j--;			
		}		
		if(str1[0]!=' ')           
		{
			if(isalpha(str1[0]))       //string checked for an alphabet
			{
				int c=0;
				for(int k=0;k<17;k++)
				{
					if(str1==str3[k])       // matching with a keyword
					{
						c=1;
						str1.append("  ");
						key.append(str1);		//appending to keyword			
					}
				}				
				if(c==0)                    //else a identifier
				{
					str1.append("  ");
					ident.append(str1);			//appending to identifier
				}
			}
			
		    else if(isdigit(str1[0]))    //checked for a digit
			{
				str1.append("  ");
				consta.append(str1);     //appending to constants
			}
			
			else
			{
				int c=0;
				for(int k=0;k<9;k++)
				{
	                         if(str1==str4[k])           //check for an operator
					{
						str1.append("  ");
						c=1;
						operat.append(str1);     //appending to operator
					}				
				}
				if(c==0)                         //else just a symbol
				{
					symb.append(str1);           //appending to symbol
				}
			}
		}		
		i++;
		str1="";		
		j--;
	}	
[/code]
Last edited on
$ gdb a.out
> run
(crash)
> backtrace


Provide a test case

Also, main must return int
sorry ne555 can you plz ellaborate what to do with the code yu have given
execute it.
get a debugger and figure out the line that produces the error.

also, provide an input/output example.
thanks i found the problem the program was acting wrongly for a double or more space in the input string .
Topic archived. No new replies allowed.