Code Problem

Well actually output problem. It compiles and links fine, but output is definitely not where it needs to be. Can someone with a "fresh set of eyes" see if you can help. Basically it's supposed to analyze the expression entered(originalExpression) and make 2 arrays, one with positions of (, the other with positions of ). But the output from cout at the end is wayy screwey. Using VS2012:
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
#include "stdafx.h"
#include <iostream>
#include <string>


int _tmain(int argc, _TCHAR* argv[])
{
	std::string originalExp;
	std::cout <<  "Enter expression: ";
	std::cin >> originalExp;
	int length = originalExp.length();
	//init 2 arrays to hold positions of ( & )
	int *temparray1 = new int[length];
	int *temparray2 = new int[length];
	// transform string to c array
	const char* temp = originalExp.c_str();
	//placeholders for loop
	int placeholder1=0;
	int placeholder2=0;
	for (int i=0; i<=length; i++){
		if (*temp+i == '('){ // look for (, and insert into array
			temparray1[placeholder1] = i;
			placeholder1++;
		}
		if (*temp+i == ')'){// look for ), and insert into array
			temparray2[placeholder2] = i;
			placeholder2++;
		}
		i++;
	}
	for (int i=0; i<length; i++){ // print array elements side by side toi test, on expression 1+(8-9)/(2*4) for example, expected result should be 2 6 & 8 12, and rest of the lines filled with 0 0.
		std::cout << temparray1[i] << " " << temparray2[i] << std::endl;
	}
	delete[] temparray1;
	delete[] temparray2;
	return 0;
}
When you have runtime problems, by far the easiest way is use the debugger. If you have an IDE it should be easy. Create a watch list of variables, create a breakpoint, step through the code 1 line at a time, see how the values change to deduce where it all goes wrong.

Having said that I can see a couple of possible problems, but it will be good practice for you to use the debugger.

Using the debugger is what happens in industry, coders don't ask their co-workers for help, or go on-line for help. It's all part of learning to be a reasonably self contained coder.

HTH
Line 20, you should be using i<length

Get rid of line 29. i is already being incremented once per loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
...
	for (int i=0; i<length; i++){
		if (*(temp+i) == '('){// look for (, and insert into array /// parentheses around temp+i
			temparray1[placeholder1] = i;
			placeholder1++;
		}
		if (*(temp+i) == ')'){// look for ), and insert into array /// parentheses around temp+i
			temparray2[placeholder2] = i;
			placeholder2++;
		}
		//i++;
	}
...
@vin & cire
ye that i was the issue can't believe I missed it, I don't even know why I put it there in a first place :/ thanks for your help

@TheIdeasMan Thanks for the advice. I usually try to figure it out myself, but after staring at it for an hour, it wasn't making sense why it wasn't working. I'm still not really used to the debugger, but I did use it on this today and it works beautifully, I didn't realize how easy it was. What was great was that I could step through each statement and monitor the result. I'm still learning lol
Last edited on
Topic archived. No new replies allowed.