Hi,

I'm currently in a C++ class and I have this one problem where you are supposed to try and create a calculator which will take the equation like this: (-insert equation here) =

I can't seem to get it to work for the life of me! I was just wondering if anyone on here can notice some logical error I have missed in my debugging. Any help would be extremely appreciated. Thanks!

[CODE=C++]#include <iostream>
using namespace std;

struct node
{
	node *previous;
	int LP;
	int RP;
	int num;
	char op;
	node *next;
};
node *additive(node *head) //need to have it stop when it hits the RP and start at the LH
{	cout<<"Checkpoint 1"<<endl;
	int result;
	node *headtemp=0;
	while(head->RP !=1)
	{
		cout<<"Checkpoint 2"<<endl;
		if(head->op== '+')
		{
			cout<<"Checkpoint Adding"<<endl;
			result=head->previous->num + head->next->num;
			cout<<result;
			headtemp=head->next->next;
			head->previous->previous->next=head->next;
			head->next->previous=head->previous->previous;
			delete head->previous;
			delete head;
			result=headtemp->num;
			cout<<headtemp->num;
		}
		else if(head->op == '-')
		{
			result=head->previous->num - head->next->num;
			cout<<result;
			headtemp=head->next;
			head->previous->previous->next=head->next;
			head->next->previous=head->previous->previous;
			delete head->previous;
			delete head;
			result=headtemp->num;
		}
		else
		{
			cout<<"Checkpoint 3"<<endl;
			return head;
		}
		head=head->next;
	}
	if(head->LP == 1)
	{	cout<<"Checkpoint 4"<<endl;
		headtemp=head->next;
		delete head;
		head=headtemp;
	}
	return head;
}

node *multiply(node *head)
{
	int result;
	node *headtemp=0;
	//if(head->op== '+' || head->op== '-')
	//{
	//	return 0;
	//}
	while(head->RP !=1)
	{
		cout<<"Checkpoint 1B"<<endl;
		if(head->op== '*')
		{
			result=head->previous->num * head->next->num;
			headtemp=head->next;
			head->previous->previous->next=head->next;
			head->next->previous=head->previous->previous;
			delete head->previous;
			delete head;
			result=headtemp->num;
		}
		else if(head->op == '/')
		{
			result=head->previous->num / head->next->num;
			headtemp=head->next;
			head->previous->previous->next=head->next;
			head->next->previous=head->previous->previous;
			delete head->previous;
			delete head;
			result=headtemp->num;
		}
		else
		{	
			cout<<"Checkpoint 2B"<<endl;
			return headtemp;
		}
	}
	if(head->LP == 1)
	{	headtemp=head->next;
		delete head;
		head=headtemp;
	}
	return headtemp;
}

int main()
{
	char s[79];
	node *head=0;
	node *temp=head;
	cout<<"Welcome to the Calculator Program! Please enter in your equation.-";
	cin>>s;
	while(s[0] != '=')
	{
		if(head == 0)
		{		
			head= new node;
			head->previous=0;
			temp=head;
		}
		else
		{
			temp->next=new node;
			temp->next->previous=temp;
			temp=temp->next;
		}
			if(isdigit(s[0]))
			{
				temp->num=atoi(s);
			}
			else if(s[0]=='(')
			{
				temp->LP=1;
				temp->op=0;
				temp->num=0;
			}
			else if(s[0]==')')
			{
				temp->RP=1;
				temp->op=0;
				temp->num=0;
			}
			else
			{

				temp->op=s[0];
				cout<<temp->op<<endl;
				temp->num=0;
			}
			cin>>s;	
	}
	temp->next=0;
	temp=head;
	while(temp->next !=0)
	{
		//head=multiply(head);
		head=additive(head);
		temp=temp->next;
	}
	cout<<head->num<<endl;
	system("pause");
	return 0;
}
Salem commented: Well done on using code tags on your first post. +16

First I would suggest you create a 'printList' function which prints out the entire list in detail, so you can easily confirm it's structure at any point in the program.

Second, start by testing known expressions.

Third, the block of code which deletes two nodes in result of evaluating 'a' op 'b' should be a common function. It's a lot of code to get right, and copy/pasting it just bloats the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.