Transferring Data between Main and a Function

Im trying to create a calculator program that simply separates the values of a string into numbers and symbols such as -+*/ etc. I'm having problems trying to updating two stacks that are sent from main to the method split() and back to main to where all the changes that occur in split() occur in the main stacks also. Any help would be amazing.

Here is how i declare it above main

void split(string,MyStack<char>&,MyStack<int>&);

Here is where i call it in main.

split(function,stackSymbols,stackNumbers);

This is my split method that i call in main.

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
void split(string str, MyStack<char>& stackSymbols, MyStack<int>& stackNumbers)
{
	string temp = "";

		for(int count = 0; count < (int)str.length(); count++)
		{
			//split the string into two different stacks
			if(str[count+0] != ' ')// space
			{
				if(str[count] == '(' || str[count] == ')' || str[count] == '+' ||
					str[count] == '-' || str[count] == '*' || str[count] == '/') //all the operand symbols
				{
					stackSymbols.addElement(str[count]);
				}
				else 
				{
					temp += temp + str[count]; //assigns temp the new number
				}

			}
			if(count + 1 >= str.length()) //checks to see if there is another value
				return;

			if(str[count+1] == '(' || str[count+1] == ')' || str[count+1] == '+' ||
					str[count+1] == '-' || str[count+1] == '*')
			{
				stackNumbers.addElement(atoi(temp.c_str()));
				temp.clear();
			}
		}
}
I don't see what your problem is. I didn't check if that function actually does what you want, but it certainly operates on the stack that you haves passed it.

Maybe you should split the function up a little - for example, write a separate function to discard whitespace, to extract a token from a string, and to check whether a token is a number or an operator (or a parenthesis). And I don't know what kind of expression you are expecting, but unless you are working on terms that are fully encloses in parentheses you will have to consider the priority of an operator as well.
you can change this part as

1
2
3
4
5
else 
	{
              if( Isdigit(str[count] )
		temp = temp + str[count]; //assigns temp the new number
	}

I found out the problem in an earlier part of my code. Apparently i was using a cin instead of a getline() function to change my str string so my str string was what was messed up and not the stacks. Thank you for helping.
Topic archived. No new replies allowed.