input on postifix calculator

Hey guys,
I am seeking help/advice/pointers on this post-fix calculator I'm writing.
I'm writing it from scratch, so I cannot use any templates for stacks.
Below is my code, but before you look at it know that I'm trying to input doubles and chars. After my input I want it to recognize the doubles/operators and push/pop them off the stack. I am having a tough time with this. Thank you in advance!!
-Note my header wont be included with my posting, the stack is a stack of arrays.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>
#include <stdio.h>
#include<algorithm>
#include<cmath>


using namespace std;
#include "dstack.h"
int main()
{
  double value;
  char op;
  Dstack stack(value);
  
  
  cin >> value;
  cin>> op;
  
  while (cin.peek() != EOF)
  {
    
    if (isspace(cin.peek()))
    {
      cin.ignore();
    }
    else if (isdigit(cin.peek()))
    {
      while (cin >> value)
      {
        stack.push(value);
      }
    }
    
    else if (!isdigit(cin.peek()))
    {
      while (cin >> op)
      {
        stack.push(op);
      }
    }
    
  }
  
}


Program:

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
//dstack.cpp
//Kauffman,Tyler
//tkauffman

#include <iostream>
using namespace std;
#include "dstack.h"

Dstack::Dstack(int size)
{
  m_top = -1;
  m_size = size;
  m_values = new int [m_size];
}
Dstack::~Dstack()
{
  delete [] m_values;
}
void Dstack::push(double value)
{
  if ((m_top+1) <m_size)
  {
    m_top++;
    m_values[m_top] = value;
  }
  else
  {
    int *new_values = new int [m_size *2];
    for(int i=0;i<m_size; i++)
    {
      new_values[i] = m_values[i];
    }
    delete [] m_values;
    m_values = new_values;
    m_size = m_size *2;
    push(value);
  }
}
bool Dstack::pop(double &value)
{
  if(m_top == -1)
    return false;
  else
  {
    value = m_values[m_top];
    m_top--;
    return true;
  }
}






What error or sympton you got?

In main.cpp
#14 used un-initialized "value" which I guess it supposed to be the first input number in "cin". Anyway, the value shouldn't be used to init object before it has some value.


#29 may be changed to "cin >> value" simply since you just need to put the input one by one. Same with #37.

#25 may be changed to "break" to quit the loop.

Hopefully I didn't misunderstand your coding.

B2ee
Topic archived. No new replies allowed.