Hi I need to know why I got these error for the infix to postfix program

So my task is to change from infix to postfix by using stack. However I got these weird problems throughout my programs

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Driver File:
#ifndef H_INFIXTOPOSTFIX
#define H_INFIXTOPOSTFIX
#include "myStack.h"
#include <string>
using namespace std;

class infixToPostfix
{
	private:
		string infix;
		string postfix;
	public: 
		void getInfix(string inFxData);
		void showInfix();
		void showPostfix();
		bool precedence(char char1, char char2);
		bool isOperand(char c);
		void convertToPostfix();
		infixToPostfix();//constructor
};
#endif

_________________________________________________________

Implementation File:
#include "myStack.h"
#include "infixToPostfix.h"
using namespace std;

infixToPostfix::infixToPostfix()
{
	infix = "";
	postfix = "";
}
void infixToPostfix::getInfix(string inFxData)
{
	infix = inFxData;
}

void infixToPostfix::showInfix()
{
	cout << "Infix: " << infix << endl;
}

void infixToPostfix::showPostfix()
{
	cout << "Postfix: "<< postfix <<endl;
}

bool infixToPostfix::precedence(char char1, char char2)
{
   if(char1 == '-' || char1 == '+')
   {
        return char2 == '+' || char2 == '-';
   }
   if(char1 == '*' || char1 == '/')
   {
           return char2 == '+' || char2 == '-' || char2 == '*' || char2 == '/';
   }
}

bool infixToPostfix::isOperand(char c)
{
   return c!= '(' && c != ')' && c != '+' && c != '-' && c != '*' && c != '/';
}

void infixToPostfix::convertToPostfix()
{
   stackType<char> myStack;
   myStack.initializeStack();
   char char1, char2;
   for(int i = 0; i < infix.size(); ++i)
   {
      char1 = infix[i];
      if(char1 == ' ')
	  {
        continue;
      }
      if(isOperand(char1))
	  {
        postfix += char1;
      }
      else if(char1 == '(')
	  {
        myStack.push(char1);
      }
      else if(char1 == ')')
	  {
		char2 = myStack.top();
             while(char2 != '(')
			 {
                   postfix += char2;
                   myStack.pop();
                   char2 = myStack.top();
             }
               myStack.pop();
      }
      else{
               if(!myStack.isEmptyStack())
			   {
                   char2 = myStack.top();
                   while(char2 != '(' && precedence(char2, char1))
				   {
                       postfix += char2;
                       myStack.pop();
                       if(!myStack.isEmptyStack())
					   {
                           char2 = myStack.top();
                       }
                       else
					   {
                           break;
                       }
                   }
               }
               myStack.push(char1);
           }
       }
       while(!myStack.isEmptyStack()){
           postfix += myStack.top();
           myStack.pop();
       }
}

___________________________________________________


_________________________________________________________________
Main.cpp
#include "myStack.h"
#include "infixToPostfix.h"
#include "stackADT.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string infix;
	fstream infile("textData.txt");
	
	if(infile.good() && infile.is_open())
	{
		while (!infile.eof())
		{
			getline(infile, infix);
			infixToPostfix inFixObj;
			inFixObj.getInfix(infix);
			inFixObj.convertToPostfix(infix);
			inFixObj.showInfix(infix);
			inFixObj.showPostfix(infix);
			
		}
	}
	return 0;
}


myStack.h
//Header file: myStack.h

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

#include "stackADT.h"

using namespace std;


template <class Type>
class stackType: public stackADT<Type>
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
//Overload the assignment operator.

void initializeStack();
//Function to initialize the stack to an empty state.
//Postcondition: stackTop = 0;

bool isEmptyStack() const;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
// otherwise returns false.

bool isFullStack() const;
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
// otherwise returns false.

void push(const Type& newItem);
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem is
// added to the top of the stack.

Type top() const;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element of the stack
// is returned.

void pop();
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top element is
// removed from the stack.


stackType(int stackSize = 100);
//Constructor
//Create an array of the size stackSize to hold
//the stack elements. The default stack size is 100.
//Postcondition: The variable list contains the base address
// of the array, stackTop = 0, and maxStackSize = stackSize

stackType(const stackType<Type>& otherStack);
//Copy constructor

~stackType();
//Destructor
//Remove all the elements from the stack.
//Postcondition: The array (list) holding the stack
// elements is deleted.

private:
int maxStackSize; //variable to store the maximum stack size
int stackTop; //variable to point to the top of the stack
Type *list; //pointer to the array that holds the stack elements

void copyStack(const stackType<Type>& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is created and assigned
// to this stack.
};

template <class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}//end initializeStack

template <class Type>
bool stackType<Type>::isEmptyStack() const
{
return(stackTop == 0);
}//end isEmptyStack

template <class Type>
bool stackType<Type>::isFullStack() const
{
return(stackTop == maxStackSize);
} //end isFullStack

template <class Type>
void stackType<Type>::push(const Type& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem; //add newItem to the
//top of the stack
stackTop++; //increment stackTop
}
else
cout << "Cannot add to a full stack." << endl;
}//end push

template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top

template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop

template <class Type>
stackType<Type>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must "
<< "be positive." << endl;
cout << "Creating an array of size 100." << endl;

maxStackSize = 100;
}
else
maxStackSize = stackSize; //set the stack size to
//the value specified by
//the parameter stackSize

stackTop = 0; //set stackTop to 0
list = new Type[maxStackSize]; //create the array to
//hold the stack elements
}//end constructor

template <class Type>
stackType<Type>::~stackType() //destructor
{
delete [] list; //deallocate the memory occupied
//by the array
}//end destructor

template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;

list = new Type[maxStackSize];

//copy otherStack into this stack
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack


template <class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;

copyStack(otherStack);
}//end copy constructor

template <class Type>
const stackType<Type>& stackType<Type>::operator=
(const stackType<Type>& otherStack)
{
if (this != &otherStack) //avoid self-copy
copyStack(otherStack);

return *this;
} //end operator=

#endif
--------------------------------------------------------------------------------------
//Header file: stackADT.h

#ifndef H_StackADT
#define H_StackADT

template <class Type>
class stackADT
{
public:
virtual void initializeStack() = 0;
//Method to initialize the stack to an empty state.
//Postcondition: Stack is empty.

virtual bool isEmptyStack() const = 0;
//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty,
// otherwise returns false.

virtual bool isFullStack() const = 0;
//Function to determine whether the stack is full.
//Postcondition: Returns true if the stack is full,
// otherwise returns false.

virtual void push(const Type& newItem) = 0;
//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem is added
// to the top of the stack.

virtual Type top() const = 0;
//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top element of the stack
// is returned.

virtual void pop() = 0;
//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top element
// is removed from the stack.
};

#endif
here is the file
testData.txt
A+B-C;
(A+B)*C;
(A+B)*(C-D);
A+((B+C)*(E-F)-G)/(H-I);
A+B*(C+D)-E/F*G+H;
Little issues. Typos. The compiler was already pointing them out. I’ve capitalised files and classes, so you can compare the two codes even by copying&pasting without interferences (but a MS Windows OS won't distinguish uppercases from lowercases):

testData.txt:
A+B-C;
(A+B)*C;
(A+B)*(C-D);
A+((B+C)*(E-F)-G)/(H-I);
A+B*(C+D)-E/F*G+H;


StackADT.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef H_StackADT
#define H_StackADT

template <class Type>
class StackADT {
public:
    virtual void initializeStack() = 0;
    virtual bool isEmptyStack() const = 0;
    virtual bool isFullStack() const = 0;
    virtual void push(const Type& newItem) = 0;
    virtual Type top() const = 0;
    virtual void pop() = 0;
};

#endif 


MyStack.h:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef H_StackType
#define H_StackType

#include "StackADT.h"
#include <iostream>
#include <cassert>

template <typename Type>
class StackType : public StackADT<Type> {
public:
    StackType(int stackSize = 100);
    StackType(const StackType<Type>& otherStack);
    ~StackType();
    const StackType<Type>& operator=(const StackType<Type>&);
    void initializeStack() override; // Postcondition: stackTop = 0;
    bool isEmptyStack() const override;
    bool isFullStack() const override;
    void push(const Type& newItem) override; // add newItem to the top of the stack.
    Type top() const override; // return the top element of the stack
    void pop() override; // remove the top element of the stack.

private:
    int maxStackSize; // maximum stack size
    int stackTop; // top of the stack
    Type *list; // pointer to the array that holds the stack elements <-- ???

    void copyStack(const StackType<Type>& otherStack); // ???
};

template <typename Type>
void StackType<Type>::initializeStack() { stackTop = 0; }

template <typename Type>
bool StackType<Type>::isEmptyStack() const { return (stackTop == 0); }

template <typename Type>
bool StackType<Type>::isFullStack() const { return(stackTop == maxStackSize); }

template <typename Type>
void StackType<Type>::push(const Type& newItem)
{
    if (!isFullStack())
    {
        list[stackTop] = newItem; //add newItem to the top of the stack
        stackTop++; //increment stackTop
    }
    else { std::cout << "Cannot add to a full stack.\n"; }
}

template <typename Type>
Type StackType<Type>::top() const
{
    assert(stackTop != 0); // if stack is empty, terminate the program <-- ??? really?
    return list[stackTop - 1]; //return the one bust last element of the stack
}

template <typename Type>
void StackType<Type>::pop()
{
    if (!isEmptyStack()) { stackTop--; }
    else                 { std::cout << "Cannot remove from an empty stack.\n"; }
}

template <typename Type>
StackType<Type>::StackType(int stackSize)
{
    if (stackSize <= 0)
    {
        std::cout << "Size of the array to hold the stack must be positive.\n"
                     "Creating an array of size 100.\n";
        maxStackSize = 100;
    }
    else { maxStackSize = stackSize; }
    stackTop = 0;
    list = new Type[maxStackSize];
}

template <typename Type>
StackType<Type>::~StackType() { delete [] list; }

template <typename Type>
void StackType<Type>::copyStack(const StackType<Type>& otherStack)
{
    delete [] list;
    maxStackSize = otherStack.maxStackSize;
    stackTop = otherStack.stackTop;

    list = new Type[maxStackSize];

    //copy otherStack into this stack
    for (int j = 0; j < stackTop; j++) { list[j] = otherStack.list[j]; }
}

template <typename Type>
StackType<Type>::StackType(const StackType<Type>& otherStack)
{
    list = nullptr;

    copyStack(otherStack);
}

template <typename Type>
const StackType<Type>& StackType<Type>::operator=(const StackType<Type>& otherStack)
{
    //avoid self-copy
    if (this != &otherStack) { copyStack(otherStack); }
    return *this;
}

#endif 


InfixToPostfix.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef H_INFIXTOPOSTFIX
#define H_INFIXTOPOSTFIX

#include <string>
#include "MyStack.h"

class InfixToPostfix {
private:
    std::string infix;
    std::string postfix;
public: 
    InfixToPostfix();
    void getInfix(std::string inFxData);
    void showInfix();
    void showPostfix();
    bool precedence(char char1, char char2);
    bool isOperand(char c);
    void convertToPostfix();
};

#endif 


InfixToPostfix.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "InfixToPostfix.h"
#include "MyStack.h"
#include <string>

InfixToPostfix::InfixToPostfix() {}

void InfixToPostfix::getInfix(std::string inFxData) { infix = inFxData; }

void InfixToPostfix::showInfix() { std::cout << "Infix: " << infix << '\n'; }

void InfixToPostfix::showPostfix() { std::cout << "Postfix: "<< postfix <<'\n'; }

bool InfixToPostfix::precedence(char char1, char char2)
{
    if(char1 == '-' || char1 == '+') { return char2 == '+' || char2 == '-'; }
    if(char1 == '*' || char1 == '/') {
        return char2 == '+' || char2 == '-' || char2 == '*' || char2 == '/';
    }
    // what if char1 is none of the above?
}

bool InfixToPostfix::isOperand(char c)
{
    return c != '(' && c != ')' && c != '+' && c != '-' && c != '*' && c != '/';
}

void InfixToPostfix::convertToPostfix()
{
    StackType<char> MyStack;
    MyStack.initializeStack();
    char char1 {'\0'}, char2 {'\0'};
    for(size_t i = 0; i < infix.size(); ++i)
    {
        char1 = infix[i];
        if     (char1 == ' ')     { continue; }
        if     (isOperand(char1)) { postfix += char1; }
        else if(char1 == '(')     { MyStack.push(char1); }
        else if(char1 == ')')
        {
            char2 = MyStack.top();
            while(char2 != '(')
            {
                postfix += char2;
                MyStack.pop();
                char2 = MyStack.top();
            }
            MyStack.pop();
        }
        else {
            if(!MyStack.isEmptyStack())
            {
                char2 = MyStack.top();
                while(char2 != '(' && precedence(char2, char1))
                {
                    postfix += char2;
                    MyStack.pop();
                    if(!MyStack.isEmptyStack())
                    {
                        char2 = MyStack.top();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            MyStack.push(char1);
        }
    }
    while(!MyStack.isEmptyStack()) {
        postfix += MyStack.top();
        MyStack.pop();
    }
}


main.cpp:
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
#include "MyStack.h"
#include "InfixToPostfix.h"
#include "StackADT.h"
#include <fstream>
#include <iostream>
#include <limits>
#include <string>

void waitForEnter();

int main()
{
    std::fstream infile("testData.txt");
    if(!infile) {
        std::cout << "Can't open data file.\nExiting now.\n";
        return 1;
    }
    for(std::string infix; std::getline(infile, infix); /**/)
    {
        InfixToPostfix inFixObj;
        inFixObj.getInfix(infix);
        inFixObj.convertToPostfix();
        inFixObj.showInfix();
        inFixObj.showPostfix();
    }
    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Output:
Infix: A+B-C;
Postfix: AB+C;-
Infix: (A+B)*C;
Postfix: AB+C;*
Infix: (A+B)*(C-D);
Postfix: AB+CD-;*
Infix: A+((B+C)*(E-F)-G)/(H-I);
Postfix: ABC+EF-*G-HI-;/+
Infix: A+B*(C+D)-E/F*G+H;
Postfix: ABCD+*+EF/G*-H;+

Press ENTER to continue...

Thks @Enoizat. It seems like you are the only one answering to my questions honestly. LOL
Don’t feel lonely, tieuholy :-D
Have you noticed that gear-like icon on the right of the code boxes? If you click there, you can compile and run the code on an online environment. It’s what many of the users of this forum do.
If you post a code split in several files, instead, we need to re-create the same structure on our local hard disks, and many of us perhaps don’t have the time to do that.

To get more answers, maybe from those real experts who attend this forum, you’d better post a... ‘one-click runnable code’- I mean: a code which fits in a single file and doesn’t need to access other files.

Anyway, in my country there’s a saying which in English could more or less be: a little of something is anyway better than nothing at all! ;-)
Happy coding!
Topic archived. No new replies allowed.