2 Replies - 1256 Views - Last Post: 01 May 2007 - 07:34 PM Rate Topic: -----

#1 enginfear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 07

Problem with my output (repeats line)

Posted 01 May 2007 - 02:59 PM

Here is what I've got, the program itself works perfect, however there is a slight problem with the output-

#include <iostream>
using namespace std;

struct node
{
	int num;
	char op;
	char type;
	node *next;
};

void printList(const node *h);

int main()
{
	node *tempPQ, *headPQ=0;
	char str[15];

	headPQ = new node;
	tempPQ = headPQ;

	while (1)
	{
		cout<<"Enter desired operation in RPN followed by '=':  "<<flush;
		cin>>str; 
		if (str[0]=='=')
			break;
			
		if (isdigit(str[0]))
		{
			tempPQ->num=atoi(str);
			tempPQ->type='N';
		}
		else
		{
			tempPQ->op=str[0];
			tempPQ->type='O';
		}

		tempPQ->next = new node;
		tempPQ = tempPQ->next;
	}

	tempPQ->next=0;
	cout<<endl;

	node *headES=0;
	int x,sum=0;

	while(headPQ->next!=0)
	{
		if(headPQ->type=='N')
		{
			x=headPQ->num;
			node *tempES = new node;
			tempES->num = x;
			tempES->next = headES;
			headES = tempES;
			
		}
		if(headPQ->type=='O')
		{
			int i,arg[2];
			for(i=0;i<2;i++)
			{
				arg[i] = headES->num;
				headES = headES->next;
			}
			switch(headPQ->op)
			{
			case '+':
				sum=arg[1]+arg[0];
				break;
			case '-':
				sum=arg[1]-arg[0];
				break;
			case '*':
				sum=arg[1]*arg[0];
				break;
			case '/':
				sum=arg[1]/arg[0];
				break;
			default:
				cout<<"Error!"<<endl;
				break;
			}
			node *tempES = new node;
			tempES->num=sum;
			
			tempES->next= headES;

			headES = tempES;			
		}

		headPQ = headPQ->next;

	}
	printList(headES);
	return 0;
}

void printList(const node *h)
{
	for (const node *p=h;p;p=p->next)
		cout<<"Solution: "<<p->num<<endl;
}




My output-
Enter desired operation in RPN followed by '=':  13 4 - =
Enter desired operation in RPN followed by '=':  Enter desired operation in RPN
followed by '=':  Enter desired operation in RPN followed by '=':
Solution: 9
Press any key to continue


Basically what I'd like for the output is this-
Enter desired operation in RPN followed by '=':  13 4 - =
Solution: 9
Press any key to continue


Any help would be appreciated, it seems like it will be a simple error to fix, I just cannot find it.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my output (repeats line)

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: Problem with my output (repeats line)

Posted 01 May 2007 - 03:06 PM

This is just a guess, but could it have something to do with your while(1) loop, that checks if the value entered is a digit or not? It might be looping through each piece of the string as it checks it.
Was This Post Helpful? 0
  • +
  • -

#3 enginfear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-April 07

Re: Problem with my output (repeats line)

Posted 01 May 2007 - 07:34 PM

I tried changing the while loop to

while(str[0]!='=')
{
		cout<<"Enter desired operation in RPN followed by '=':  "<<flush;
		cin>>str; 
	
		if (str[0]=='=')
			break;
			
		if (isdigit(str[0]))
		{
			tempPQ->num=atoi(str);
			tempPQ->type='N';
		}
		else
		{
			tempPQ->op=str[0];
			tempPQ->type='O';
		}

		tempPQ->next = new node;
		tempPQ = tempPQ->next;
}


But I still get the same result of repetition. I feel the problem is in this loop beucase however many numbers I enter is how many times it repeats. For example if I put "13 4 - =" it will repeat 3 times.

This post has been edited by enginfear: 01 May 2007 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1