-7

So this is a homework I have for my university. I have to read a polish notation from a line in a text file. The expression in the text file is: "/ 2 3 " and i have to turn it into a reversed polish notation. I keep getting this error: 0xC0000005: Access violation reading location 0x00000000.

int top = -1;
char prefix[50];
void Push(char value)
{
    top++;
prefix[top] = value;
}
void Pop()
{
if (top < 0)
    cout << "The stack is empty." << endl;
else
{
    top--;
}
}

char Top()
{
return prefix[top];
}

void Display()
{
for (int i = 0; i <= top; i++)
    cout << prefix[i] << " ";
}

bool isOperator(char c)
{
if (c == '+' || c == '-' || c == '*' || c == '/')
    return true;
else
    return false;
}

char c;
char postfix[50];
int top2 = -1;
void Push2()
{
top2++;
postfix[top2] = Top() + Top() + c;
}

void Display2()
{
{
    for (int i = 0; i <= top2; i++)
    cout << postfix[i] << " ";
}
};
void PrefixToPostfix()
{

for (int *i = 0; *i <= top2; i++)
{
    c = prefix[*i];
    if (isOperator)
    {
        Push2();
    }
    else
    {
        top2++;
        postfix[top2] = c;
    }

}
}

int _tmain(int argc, _TCHAR* argv[])
{
char value;
char c;

ifstream file("Prefix.txt");
while (file >> value)
{
    Push(value);
}
file.close();
PrefixToPostfix();
Display();
Display2();
cout << endl;
system("PAUSE");
return 0;
} 

i think the error might be in this part of my code:

void PrefixToPostfix()
{

for (int *i = 0; *i <= top2; i++)
    {
        c = prefix[*i];

If someone can help me, I would be very thankful because i have to turn in my homework in 5 hours. :)

  • http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen Nov 11 '16 at 15:21
  • 1
    Possible duplicate of [0xC0000005: Access violation reading location 0x00000000](http://stackoverflow.com/questions/10478941/0xc0000005-access-violation-reading-location-0x00000000) – Marvin Nov 11 '16 at 15:24
  • 1
    Please stop trying to learn C++ by trial and error, it will get you nowhere. Learn it systematically from a good book instead. – Baum mit Augen Nov 11 '16 at 15:25
  • You are definitely stepping into uninitialized memory. Like your `prefix` array. Your for loops look suspicious. – AndyG Nov 11 '16 at 15:25
  • 2
    This `for (int *i = 0` makes `i` a NULL pointer, and the next `*i` will fail. – Bo Persson Nov 11 '16 at 15:30

1 Answers1

2

There is no need for a pointer here:

for (int i = 0; i <= top2; i++) {
  c = prefix[i]; }
Biffen
  • 6,249
  • 6
  • 28
  • 36
pepece
  • 360
  • 5
  • 17