0 does not exit

I've been reading my code for the past 3 days and I cannot get 0 to exit the program. Any solution?

#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>

using namespace std;

size_t dub_count = 0;
size_t op_count = 0;

double answer = 0;
bool keep_going = true;
void reset();
void test_stacks();
void calculate();
struct dub_node
{
double data;
dub_node *next;
};

dub_node *dub_head = NULL;

void push_dub(double input)
{
dub_node *temp, *temp1;
temp1 = dub_head;
temp = new dub_node;
temp->data = input;
temp->next = dub_head;
dub_head = temp;
dub_count++;
}

double dub_peek()
{
if (dub_head != NULL)
return dub_head->data;
else return 9999999999.98739;
}

double dub_pop()
{
dub_node *temp;
if (dub_head == NULL)
{
return 9999999999.98739;
}
else
{
temp = dub_head;
dub_head = temp->next;
double ret_value = temp->data;
delete temp;
--dub_count;
return ret_value;
}
}

struct op_node
{
char data;
op_node *next;

};

op_node *op_head;

void push_op(char input)
{
op_node *temp, *temp1;
temp1 = op_head;
temp = new op_node;
temp->data = input;
temp->next = op_head;
op_head = temp;
op_count++;
}

char op_pop()
{
op_node *temp;
if (op_head == NULL)
{
return 'f';
}
else
{
temp = op_head;
op_head = temp->next;
char ret_value = temp->data;
delete temp;
--op_count;
return ret_value;
}
}

char op_peek()
{
if (op_head != NULL)
return op_head->data;
else return 'f';
}

void test_stacks()
{
if (op_count + 1 > dub_count)
{
cout << "Error: To many operators. Please try again.\n";
reset();
}
else if (op_count + 1 < dub_count)
{
cout << "Error: To many operands. Please try again.\n";
reset();
}
else
{
calculate();
}
}

void read_and_push(istream &ins)
{
const char DECIMAL = '.';
double number;
char op;

if (ins.peek() == 0){ keep_going = false; }
else
{
while (ins.peek() != '\n')
{
if (isdigit(ins.peek()) || ins.peek() == DECIMAL)
{
ins >> number;
push_dub(number);
}
else if (ins.peek() == '+' || ins.peek() == '-' || ins.peek() == '*' || ins.peek() == '/')
{
ins >> op;
push_op(op);
}
else
ins.ignore();
}
ins.ignore();
}
}

void reset()
{
char op_cursor;
op_cursor = op_peek();
while (op_cursor != 'f')
{
op_cursor = op_pop();
}
double dub_cursor;
dub_cursor = dub_peek();
while (dub_cursor != 9999999999.98739)
{
dub_cursor = dub_pop();
}
dub_head = NULL;
op_head = NULL;
dub_count = 0;
op_count = 0;
}

void calculate()
{
double operator1, operator2;
char operand;
do
{
answer = 0;
operator2 = dub_pop();
operator1 = dub_pop();
operand = op_pop();
switch (operand)
{
case '+':
answer = operator2 + operator1;
push_dub(answer);
break;
case '-':
answer = operator2 - operator1;
push_dub(answer);
break;
case '*':
answer = operator2 * operator1;
push_dub(answer);
break;
case '/':
if (operator1 == 0)
{
cout << "Error: Division by 0. Please try again.\n";
reset();
}
else
answer = operator2 / operator1;
push_dub(answer);
break;
}
} while (op_peek() != 'f');
}

int main()
{
cout << "Please input equations\n";
while (keep_going)
{
cout << "Type 0 to exit\n";
read_and_push(cin);
test_stacks();
cout << "\nAnswer is: " << dub_pop() << "\n";
reset();
};
return 0;
}
Why is your program structured this way? You should validate input in main before proceeding to call any other function.

Global variables are bad.
I'm still new to programming. Do you know how I could make it so 0 will exit?
1
2
3
4
5
6
7
8
9
10
while(true)
{
    int n;
    std::cin >> n;
    if(n == 0)
    {
        break;
    }
    //...
}
ne555's suggestion below ie better.
Last edited on
1
2
3
int n;
while( std::cin>>n and n not_eq 0 )
  //... 
Topic archived. No new replies allowed.