spacing check trouble

hello i ran into some trouble with my function. we are required to check if the spacing is valid in our input. what i was trying to do is first check if the very first space is a space which works correectly then i was trying to check when it gets to a parenthesis operator or operand it will check the element before and after it for a space and then say wether its true or not i tried testing different things and labeled my lines to see where it goes wrong but i have no clue as to why its not working any help would be appreciated

here is a picture of my output and my input file http://i.imgur.com/Q4GYW6n.png
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
  void spacecheck(string data)
{
	if (data[0] == ' ')
	{
		cout << "correct" << endl;
	}
	else if (data[0] != ' ')
	{
		cout << "ERROR spaces incorrect ending program." << endl;
		system("pause");
		exit(0);
	}
	for (int i = 1; i <= data.length(); i++)
	{
		 if (data[i] == '(' || data[i] == ')' || data[i] == '+' || data[i] == '-' || data[i] == '/' || data[i] == '*'){
			data[i++] == ' ';
			cout << "correct spacing    1" << endl;	
		}
		else if (data[i] >= 'A' && data[i] <= 'Z'){
			data[i++] == ' ';
			cout << "correct spacing    2" << endl;
		}

		else if (data[i] == '(' || data[i] == ')' || data[i] == '+' || data[i] == '-' || data[i] == '/' || data[i] == '*'){
			data[i++] != ' ';
			cout << data[i] << " line 3" << endl;
			cout << "ERROR spaces incorrect ending program." << endl;
			system("pause");
			exit(0);
		}
		else if (data[i] >= 'A' && data[i] <= 'Z'){
			data[i++] != ' ';
			cout << "ERROR spaces incorrect ending program." << endl;
			system("pause");
			exit(0);
		}
		else if (data[i] = ' ')
		{
			cout << "heh";
		}
		
	}
	for (int i = 1; i <= data.size(); i++)
	{
	if (data[i] == '(' || data[i] == ')' || data[i] == '+' || data[i] == '-' || data[i] == '/' || data[i] == '*'){
		data[i--] != ' ';
		cout << data[i] << " line 2" << endl;
		cout << "ERROR spaces incorrect ending program." << endl;
		system("pause");
		exit(0);
	}
		else if (data[i] == '(' || data[i] == ')' || data[i] == '+' || data[i] == '-' || data[i] == '/' || data[i] == '*'){
			data[i--] == ' ';
			cout << "correct spacing 1" << endl;
		}
		else if (data[i] >= 'A' && data[i] <= 'Z'){
			data[i--] == ' ';
			cout << "correct spacing 2" << endl;
		}
	
		else if (data[i] >= 'A' && data[i] <= 'Z'){
			data[i--] != ' ';
			cout << "ERROR spaces incorrect ending program." << endl;
			system("pause");
			exit(0);
		}
		else if(data[i] = ' ')
		{
			cout << "heh";
		}
			
	}
	cout << "correct spacing 3" << endl;
	cout << endl;
}
Here's what you have...
If you were to take " A + B " you would first read the space, data[0] but do nothing since it's correct. Next data[A] satisfies >='A'...., so we go into the else if statement. In there you check data[A] again, THEN decrement i (but i gets updated during the next iteration so the i-- or i++ is overwritten every time). Here, is where nothing happens. Then cout and end.

Where you have i++ and i-- in data[] you wrote it so it's post operations. That means it will evaluate with current i and then increment/decrement after. You can simply write i+1 and i-1 since the space you're checking, i, is being updated after each loop, i.e. advancing to the next character in your string.

Also, your data[i]==' ' statements aren't in any condition statements. Try using a nested statement. For example, if you read in an operator, operand, parentheses, as you suggested, you'll want to check IF the previous and IF the following are spaces. If not, then display your ERROR message.
Last edited on
@danghotties i changed those -- and ++ to -1 and +1 as you said im not following what your are trying to say in your last paragraph could you explain it some more and gimmie an example?
@danghotties i actualy got it to work with your advice i finaly realized what you meant thanks alot :D
You want to check if the following or preceding is a space so you need some kind of condition to check that. Try placing data[i+(or -)1] in an if statement like this section of your code. For example:
1
2
3
4
5
6
7
if (data[i] == '(' || data[i] == ')' || data[i] == '+' || data[i] == '-' || data[i] == '/' || data[i] == '*')
{
	if (data[ i +1]  == ' ' )
		cout << "correct spacing    1" << endl;
	else
		cout << "ERROR spaces incorrect ending program." << endl;
}
Last edited on
Topic archived. No new replies allowed.