c++ using stacks problem

I am working on a C++ program using stacks. The program is to check and see if an experssion entered by the user is well-formed or not. There are 3 different settings, 1. Basic brackets () 2. Stanard brackets (){}[] and 3. User-defind brackets. I have Three files main.cpp, stackLS.cpp, stackLS.h. It's a simple program that should only tell the user if the expression they entered is well-formed or not. Nothing more. I am having some issues though. I will past the codes below:

main.cpp (what I have came up with so far)

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
#include <iostream>
 #include <string>
 #include "StackLS.h"
 using namespace std;
 

int main()
 {
 int choice;
 char answer;
 char n;
 StackLS stack;
 

cout << " ********** MENU ********** " << endl;
 cout << " 1. Basic Brackets () " << endl;
 cout << " 2. Standard Brackets ()[]{} " << endl;
 cout << " 3. User-Defined brackets " << endl;
 cout << " Please enter your choice: " << endl;
 
switch (choice){
 case 1: 
cout << "Current Setting: () " << endl;
 cout << "Enter your expression followed by a ; : " << endl;
 cin >> answer;
 
do { 

if (answer == '(')
 stack.push( '(' );
 else 
if (answer = ')' )
 (stack.top() == '(');
 }
 while (answer != ';');
 stack.pop();
 }
 
} // end main 

stackLS.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
75
76
77
78
79
80
81
82
83
#include "StackLS.h"
 
// constructors
 StackLS::StackLS(void)
 // default constructor
 {
 topItem = 0;
 } // end default constructor
 
StackLS::StackLS(const StackLS& aStack)
 // copy constructor
 {
 } // end copy constructor
 
// observers
 bool StackLS::isEmpty(void) const
 // returns true if this stack is empty
 // false otherwise
 {
 return topItem == 0;
 } // end isEmpty
 
bool StackLS::isFull(void) const
 // returns true if this stack is full
 // false otherwise
 {
 return false;
 } // end isFull
 
elemType StackLS::top(void) const
 // precondition: this stack is not empty
 // returns top element in this stack
 {
 // return (*topItem).data;
 return topItem->data;
 } // end top
 
// transformers
 void StackLS::push(const elemType& item)
 // precondition: this stack is not full
 // adds item to this stack
 {
 Node *newNode = new Node;
 newNode->data = item;
 newNode->next = topItem;
 topItem = newNode;
 } // end push
 
void StackLS::pop(void)
 // removes top element from this stack if exist
 // remains empty otherwise
 {
 if (topItem != 0)
 {
 Node *temp = topItem;
 topItem = topItem->next;
 delete temp;
 }
 } // end pop
 
void StackLS::makeEmpty(void)
 // makes this stack empty
 {
 while (topItem != 0)
 {
 Node *temp = topItem;
 topItem = topItem->next;
 delete temp;
 }
 } // end makeEmpty
 
// destructor
 StackLS::~StackLS(void)
 {
 //while (!isEmpty())
 // pop();
 while (topItem != 0)
 {
 Node *temp = topItem;
 topItem = topItem->next;
 delete temp;
 }
 } // end destructor 


stackLS.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
#pragma once
 #ifndef _STACKLS_
 #define _STACKLS_
 
#include <iostream>
 using namespace std;
 
typedef char elemType; // flexible data type
 
class StackLS
 {
 private:
 // inner class node
 
class Node
 {
 public:
 elemType data; // data portion
 Node *next; // link to the seccessor
 }; // end Node
 
// data members
 Node *topItem; // pointer to the top element of this stack
 
// utilities
 
public:
 // constructors
 StackLS(void); // default constructor
 StackLS(const StackLS& aStack); // copy constructor
 
// observers
 bool isEmpty(void) const;
 // returns true if this stack is empty
 // false otherwise
 
bool isFull(void) const;
 // returns true if this stack is full
 // false otherwise
 
elemType top(void) const;
 // precondition: this stack is not empty
 // returns top element in this stack
 
// transformers
 void push(const elemType& item);
 // precondition: this stack is not full
 // adds item to this stack
 
void pop(void);
 // removes top element from this stack if exist
 // remains empty otherwise
 
void makeEmpty(void);
 // makes this stack empty
 
// destructor
 ~StackLS(void);
 }; // end StackLS
 
#endif 

If someone could please help me get it started, I would appreciate it. I have some started in the main.cpp but I know that it is not right. I'm stuck on what to do. Thanks again. If you need more information, let me know what information and I will add it to here. Thanks
Last edited on
you have some issue in main.cpp:

On line 33: What's your intention? It compares top with '(' and then?

Line 25 is outside the loop and hence you have an infinite loop or nothing happens at all. Becauses 'answer' never changes that is
Topic archived. No new replies allowed.