Hey guys, I need help recognizing multiple digit numbers that are read so that I may push said number into a stack for future evaluation
For example: 22 3 + should evaluate to 25

When I input 22 3 +, my output is evaluated to 5

Any help/criticism is welcome

Here is what I have so far:

template <int size>
void Stack<size> :: fillStack()
{
    string expression;
   
    cout << "Enter a postfix expression and terminate it with a '$': ";
    getline(cin, expression);

	int n = expression.length();
   
    for (int i = 0; i < n; i++)
    {
        if (!isStackFull())
        {
            if (isdigit(expression[i]))
            {
                float x = (static_cast<float>(expression[i]) - '0');

				if (isdigit(expression[i+1]))
				{
					int b = i+1;
					while(isdigit(expression[b]))
					{
						x = x*10 + (static_cast<float>(expression[i+1]) - '0');
						b++;
					}

					stack[counter++] = x;
				}
				else
				{
					stack[counter++] = x;
				}
            }
            else if (ispunct(expression[i]))
	    {
		operate(expression[i]);
	    }
        }
        else
	{
	   cout << "Error: Stack is Full" << endl;
           exit(0);	}
	}
}

try parsing the expression by spaces putting every token into a string and converting to numerical values when not an operator. Operators have length one and aren't digits (assuming the compound operator like +=, /=, etc aren't allowed) and no space between negative sign and first digit (or decimal point) is allowed if value is less than zero.

If negative numbers not allowed, once you find a char that is a digit build a string of digits stopping at first non digit in the sting and conver the string to numerical value once it's completed.

If negative numbers not allowed, once you find a char that is a digit build a string of digits stopping at first non digit in the sting and conver the string to numerical value once it's completed.

do you mean something along the lines of scanning each character and pushing it into a string until I encounter a ' ' or an operator? If so, would you mind showing me a quick example?

string temp
if(isdigit(expression[i])) //if current char in expression is a digit
  temp += expression[i]  //addend this digit to temp
  ++i //go to next char in expression
  while(isdigit(expression[i]))//while next char in expressionis a digit
    temp += expression[i] //addend it to temp
    ++i;//go to next char in expression
  //end while loop
//end if statement

convert temp to numerical value

push temp on stack

--i; //decrease i by one so when i is incremented by one in the for loop you don't skip over a char in expression.

I'm having problems seeing the logic. I rewrote my function and it now looks like this. However, now I am getting a conversion error cannot convert string to float.

template <int size>
void Stack<size> :: fillStack()
{
    string expression;
	string temp;
   
    cout << "Enter a postfix expression and terminate it with a '$': "; //prompt
    getline(cin, expression);

	int n = expression.length();
	for (int i = 0; i < n; i++)
	{
		if (!isStackFull())
		{
			if (isdigit(expression[i]))
			{
				temp += expression[i];
				i++;
               
				while(isdigit(expression[i]))
				{
					temp += expression[i];
					i++;
				}
	
				float x = (static_cast<float>(temp) - '0');
				
				stack[counter++] = x;
			}
			else if (ispunct(expression[i]))
			{
				operate(expression[i]);
			}
		}	
		else
		{
			cout << "Error: Stack is Full" << endl;
			exit(0);
		}
	}
}

You can't assign a string to a float value. You have to convert the string into a floating point number. Look it up. Key word is convert

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.