#include <iostream>
#include <string>
using namespace std;
const int size = 100;
class stack{
private: // Declare a structure for the list
struct ListNode
{
char value;
struct ListNode *next;
struct ListNode *back;
};
int top;
char data[size];
ListNode *head; // List head pointer
ListNode *tail;// tail pointer
public:
stack(void) // Constructor
{
head = NULL;
tail = NULL;
}
public: // function declaration
void appendNode(char num);
void forward(void);
void deleteNode(char num);
void input(void);
void convert(void);
void createStack();
void push(char); // insert operation
char pop(); // delete operation
char stackTop(); // get top value
bool isFull(); // check if stack is Full
bool isEmpty(); // check if stack is empty
bool prcd(char symb);
};
void stack::createStack(){
top = -1;
}
bool stack::isFull()
{
return (top == size - 1);
}
bool stack::isEmpty()
{
return (top == -1);
}
void stack::push(char newitem)
{
if (isFull()){
cout << "sorry cannot push stack is full \n";
}
else {
top = top + 1;
data[top] = newitem;
}
}//end push()
char stack::pop()
{
char item;
if (isEmpty()){
cout << "Sorry, Cannot pop item.Stack is empty!" << endl;
}
else
{ //display value at top to be deleted
cout << "Popped value : " << data[top];
top -=1;
return data[top];
// top will hold to new index
}// end else
}//end pop
char stack::stackTop()
{ //function to get top value
stack obj2;
if (obj2.isEmpty()){
cout << "Sorry, stack is empty!" << endl;
}
else
return data[top];
} // end stackTop
bool stack:: prcd(char symb)//checks the precedence of the operators
{
if ((data[top] == '+') && (symb == '*'))
return false;
else if ((data[top] == '-') && (symb == '*'))
return false;
else if ((data[top] == '+') && (symb == '/'))
return false;
else if ((data[top] == '-') && (symb == '/'))
return false;
else if ((data[top] == ('+' || '-' || '*' || '/')) && (symb == '('))
return false;
else if (data[top] == '(' && symb == ('+' || '-' || '*' || '/'))
return false;
else if (data[top] == '(' && symb == ')')
return false;
else if ((data[top] != ')') && (symb == '('))
return false;
else
return true;
}
void stack::appendNode(char num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head) // head == NULL
head = newNode;
else// Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
void stack::forward(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value<< " ";
nodePtr = nodePtr->next;
}
}
void stack::deleteNode(char num)
{
ListNode *nodePtr, *previousNode=NULL;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
void stack::convert(){
ListNode *nodeptr;
stack s;
char c;
string pf='\0';
char op;
nodeptr = head;
s.createStack();
while (nodeptr){
c = nodeptr->value;
if (c == ('+' || '-' || '*' || '/' || '(' || ')')){
while (!s.isEmpty() && prcd(c)){
op = s.pop();
pf += op;
}
if (s.isEmpty() || c != ')' )
s.push(c);
elses.pop(); // discard the ‘(‘
}
else{
pf += c;
}
nodeptr = nodeptr->next;
}
while (!s.isEmpty()) {
op = s.pop();
pf += op;
}
cout << pf;
}
void stack:: input ()
{
stack obj;
char inp;
cout << "enter the required expresson\n press a to stop\n ";
cin >> inp;
obj.appendNode(inp);
while (inp != 'a'){
cin >> inp;
obj.appendNode(inp);
if (inp == 'a'){
obj.deleteNode(inp);
}
}
obj.forward();
}
void main(){
stack obj1;
obj1.input();
}
12 Replies - 1506 Views - Last Post: 28 September 2014 - 11:47 AM
#1
unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 02:00 AM
Replies To: unknown problem, no error but program stop working after 1st call
#2
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 02:21 AM
#3
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 02:23 AM
#4
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 02:24 AM
$ g++ -Wall bar.cpp bar.cpp: In member function ‘char stack::pop()’: bar.cpp:69:8: warning: unused variable ‘item’ [-Wunused-variable] bar.cpp: In member function ‘char stack::stackTop()’: bar.cpp:88:1: warning: control reaches end of non-void function [-Wreturn-type] bar.cpp: In member function ‘char stack::pop()’: bar.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]
You can make life easier for yourself (and anyone who has to read your code) by adopting a consistent approach to indentation.
Most IDEs will either do this for you automatically, or have an option called "reformat code".
#include <iostream>
#include <string>
using namespace std;
const int size = 100;
class stack {
private: // Declare a structure for the list
struct ListNode {
char value;
struct ListNode *next;
struct ListNode *back;
};
int top;
char data[size];
ListNode *head; // List head pointer
ListNode *tail; // tail pointer
public:
stack(void) // Constructor
{
head = NULL;
tail = NULL;
} public: // function declaration
void appendNode(char num);
void forward(void);
void deleteNode(char num);
void input(void);
void convert(void);
void createStack();
void push(char); // insert operation
char pop(); // delete operation
char stackTop(); // get top value
bool isFull(); // check if stack is Full
bool isEmpty(); // check if stack is empty
bool prcd(char symb);
};
void stack::createStack()
{
top = -1;
}
bool stack::isFull()
{
return (top == size - 1);
}
bool stack::isEmpty()
{
return (top == -1);
}
void stack::push(char newitem)
{
if (isFull()) {
cout << "sorry cannot push stack is full \n";
} else {
top = top + 1;
data[top] = newitem;
}
} //end push()
char stack::pop()
{
char item;
if (isEmpty()) {
cout << "Sorry, Cannot pop item.Stack is empty!" << endl;
} else { //display value at top to be deleted
cout << "Popped value : " << data[top];
top -= 1;
return data[top];
// top will hold to new index
} // end else
} //end pop
char stack::stackTop()
{ //function to get top value
stack obj2;
if (obj2.isEmpty()) {
cout << "Sorry, stack is empty!" << endl;
} else
return data[top];
} // end stackTop
bool stack::prcd(char symb) //checks the precedence of the operators
{
if ((data[top] == '+') && (symb == '*'))
return false;
else if ((data[top] == '-') && (symb == '*'))
return false;
else if ((data[top] == '+') && (symb == '/'))
return false;
else if ((data[top] == '-') && (symb == '/'))
return false;
else if ((data[top] == ('+' || '-' || '*' || '/')) && (symb == '('))
return false;
else if (data[top] == '(' && symb == ('+' || '-' || '*' || '/'))
return false;
else if (data[top] == '(' && symb == ')')
return false;
else if ((data[top] != ')') && (symb == '('))
return false;
else
return true;
}
void stack::appendNode(char num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
void stack::forward(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr) {
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
void stack::deleteNode(char num)
{
ListNode *nodePtr, *previousNode = NULL;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num) {
nodePtr = head->next;
delete head;
head = nodePtr;
} else {
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is not equal to num.
while (nodePtr != NULL && nodePtr->value != num) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
void stack::convert()
{
ListNode *nodeptr;
stack s;
char c;
string pf = '\0';
char op;
nodeptr = head;
s.createStack();
while (nodeptr) {
c = nodeptr->value;
if (c == ('+' || '-' || '*' || '/' || '(' || ')')) {
while (!s.isEmpty() && prcd(c)) {
op = s.pop();
pf += op;
}
if (s.isEmpty() || c != ')')
s.push(c);
else
s.pop(); // discard the ‘(‘
} else {
pf += c;
}
nodeptr = nodeptr->next;
}
while (!s.isEmpty()) {
op = s.pop();
pf += op;
}
cout << pf;
}
void stack::input()
{
stack obj;
char inp;
cout << "enter the required expresson\n press a to stop\n ";
cin >> inp;
obj.appendNode(inp);
while (inp != 'a') {
cin >> inp;
obj.appendNode(inp);
if (inp == 'a') {
obj.deleteNode(inp);
}
}
obj.forward();
}
int main()
{
stack obj1;
obj1.input();
}
This post has been edited by Salem_c: 28 September 2014 - 02:27 AM
#5
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 08:21 AM
#6
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 08:41 AM
i had a few errors at first but they are now gone and the program is running but after posting the input , program suddenly stopped, now it is not giving an error , and i cant find anything wrong with it can some one please tell me what have i done wrong..
thank you.
#include <iostream>
#include <string>
using namespace std;
const int size = 100;
class stack {
private: // Declare a structure for the list
struct ListNode {
char value;
struct ListNode *next;
struct ListNode *back;
};
int top;
char data[size];
ListNode *head; // List head pointer
ListNode *tail; // tail pointer
public:
stack(void) // Constructor
{
head = NULL;
tail = NULL;
} public: // function declaration
void appendNode(char num);
void forward(void);
void deleteNode(char num);
void input(void);
void convert(void);
void createStack();
void push(char); // insert operation
char pop(); // delete operation
char stackTop(); // get top value
bool isFull(); // check if stack is Full
bool isEmpty(); // check if stack is empty
bool prcd(char symb);
bool isOperator(char num);
};
void stack::createStack()
{
top = -1;
}
bool stack::isFull()
{
return (top == size - 1);
}
bool stack::isEmpty()
{
return (top == -1);
}
void stack::push(char newitem)
{
if (isFull()) {
cout << "sorry cannot push stack is full \n";
}
else {
top = top + 1;
data[top] = newitem;
}
} //end push()
char stack::pop()
{
char item;
if (isEmpty()) {
cout << "Sorry, Cannot pop item.Stack is empty!" << endl;
}
else { //display value at top to be deleted
cout << "Popped value : " << data[top];
top -= 1;
return data[top];
// top will hold to new index
} // end else
} //end pop
char stack::stackTop()
{ //function to get top value
stack obj2;
if (obj2.isEmpty()) {
cout << "Sorry, stack is empty!" << endl;
}
else
return data[top];
} // end stackTop
bool stack::prcd(char symb) //checks the precedence of the operators
{
if ((data[top] == '+') && (symb == '*'))
return false;
else if ((data[top] == '-') && (symb == '*'))
return false;
else if ((data[top] == '+') && (symb == '/'))
return false;
else if ((data[top] == '-') && (symb == '/'))
return false;
else if ((data[top] == ('+' || '-' || '*' || '/')) && (symb == '('))
return false;
else if (data[top] == '(' && symb == ('+' || '-' || '*' || '/'))
return false;
else if (data[top] == '(' && symb == ')')
return false;
else if ((data[top] != ')') && (symb == '('))
return false;
else
return true;
}
void stack::appendNode(char num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head) // head == NULL
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
void stack::forward(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr) {
cout << nodePtr->value << " ";
nodePtr = nodePtr->next;
}
}
void stack::deleteNode(char num)
{
ListNode *nodePtr, *previousNode = NULL;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->value == num) {
nodePtr = head->next;
delete head;
head = nodePtr;
}
else {
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is not equal to num.
while (nodePtr != NULL && nodePtr->value != num) {
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
bool stack::isOperator(char num){
if (num = '+')
return true;
else if (num = '-')
return true;
else if (num = '*')
return true;
else if (num = '/')
return true;
else if (num = '(')
return true;
else if (num = ')')
return true;
else
return false;
}
void stack::convert()
{
ListNode *nodeptr;
stack s;
char c;
string pf = '\0';
char op;
nodeptr = head;
s.createStack();
while (nodeptr) {
c = nodeptr->value;
if (isOperator(c)) {
while (!s.isEmpty() && prcd(c)) {
op = s.pop();
pf += op;
}
if (s.isEmpty() || c != ')')
s.push(c);
else
s.pop(); // discard the ‘(‘
}
else {
pf += c;
}
nodeptr = nodeptr->next;
}
while (!s.isEmpty()) {
op = s.pop();
pf += op;
}
cout << pf;
}
void stack::input()
{
char inp;
cout << "enter the required expresson\n press a to stop\n ";
cin >> inp;
appendNode(inp);
while (inp != 'a') {
cin >> inp;
appendNode(inp);
if (inp == 'a') {
deleteNode(inp);
}
}
forward();
convert();
}
int main()
{
stack obj;
obj.input();
}
#7
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 09:37 AM
Quote
stack.cpp: In member function ‘char stack::pop()’:
stack.cpp:67:7: warning: unused variable ‘item’ [-Wunused-variable]
char item;
^
stack.cpp: In member function ‘bool stack::isOperator(char)’:
stack.cpp:177:15: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if (num = '+')
^
stack.cpp:179:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (num = '-')
^
stack.cpp:181:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (num = '*')
^
stack.cpp:183:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (num = '/')
^
stack.cpp:185:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (num = '(')
^
stack.cpp:187:20: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
else if (num = ')')
^
stack.cpp: In member function ‘char stack::pop()’:
stack.cpp:77:1: warning: control reaches end of non-void function [-Wreturn-type]
} //end pop
^
stack.cpp: In member function ‘char stack::stackTop()’:
stack.cpp:87:1: warning: control reaches end of non-void function [-Wreturn-type]
} // end stackTop
^
Remember that there is a difference between a single assignment operator and two. Just one means its an assignment, two means its a comparison.
*EDIT*:
Please don't make duplicate topics for the same program.
This post has been edited by vividexstance: 28 September 2014 - 09:44 AM
Reason for edit:: Merged topics
#8
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 09:46 AM
#9
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 09:50 AM
What exactly happens when you run the program? Just saying it doesn't work isn't that helpful.
EDIT:
Please post your updated code?
This post has been edited by vividexstance: 28 September 2014 - 09:51 AM
#10
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 10:08 AM
This is the exception I'm getting:
Quote
what(): basic_string::_S_construct null not valid
This line is causing it (199):
string pf = '\0';
Change those single quotes to double quotes.
This post has been edited by vividexstance: 28 September 2014 - 10:08 AM
#11
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 10:08 AM
$ g++ -g -Wall bar.cpp
bar.cpp: In member function ‘char stack::pop()’:
bar.cpp:67:8: warning: unused variable ‘item’ [-Wunused-variable]
bar.cpp: In member function ‘char stack::stackTop()’:
bar.cpp:87:1: warning: control reaches end of non-void function [-Wreturn-type]
bar.cpp: In member function ‘char stack::pop()’:
bar.cpp:77:1: warning: control reaches end of non-void function [-Wreturn-type]
$ gdb -q ./a.out
Reading symbols from /home/sc/Documents/a.out...done.
(gdb) run
Starting program: /home/sc/Documents/a.out
enter the required expresson
press a to stop
1 + 2 * 3
a
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
1 + 2 * 3
Program received signal SIGABRT, Aborted.
0x00007ffff75523e5 in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
64 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
in ../nptl/sysdeps/unix/sysv/linux/raise.c
(gdb) bt
#0 0x00007ffff75523e5 in __GI_raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
#1 0x00007ffff7555b4b in __GI_abort () at abort.c:92
#2 0x00007ffff7b90d7d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffff7b8ef26 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffff7b8ef53 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffff7b8f04e in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x00007ffff7b3b537 in std::__throw_logic_error(char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7 0x00007ffff7b79ad9 in char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8 0x00007ffff7b79bb3 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) ()
from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9 0x0000000000401271 in stack::convert (this=0x7fffffffdf70) at bar.cpp:199
#10 0x0000000000401549 in stack::input (this=0x7fffffffdf70) at bar.cpp:245
#11 0x000000000040157a in main () at bar.cpp:252
This shows the program crashed at bar.cpp:199, which in this case is
string pf = '\0';
As well as analysing where it crashed, a debugger allows you to set breakpoints and examine (or even change) variables. You can single step lines of code, or just step over whole functions you know to be working.
When the code path takes an unexpected turn, or a variable has an unexpected value, then you have found a bug.
Whether this is a bug in the code, or a bug in your understanding of the problem, is for you to figure out.
#12
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 10:42 AM
#13
Re: unknown problem, no error but program stop working after 1st call
Posted 28 September 2014 - 11:47 AM
string Empty; // Create an empty string. ... Empty.clear(); // Delete everything contained in the string.
Jim

New Topic/Question
Reply


MultiQuote




|