9 Replies - 2753 Views - Last Post: 12 May 2011 - 12:08 PM Rate Topic: -----

#1 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 171
  • Joined: 05-March 09

template stacks and queues

Posted 12 May 2011 - 10:20 AM

This program is supposed to write your own templates and when the program
compiles there are no errors but it only outputs part of the desired output and
then exits in a segmentation fault
This is the output its printing out
Input is: 25 
10 

- 
100 


115 


- 
- 
300 


2 
5 
* 
/ 
/ 

Contents of input queue: 25 
10 
- 
100 
115 
- 
- 
300 
2 
5 
* 
/ 
/ 
Segmentation fault//After this is should say input reversed 



This is the code:


#ifndef MY_QUEUE_H
#define MY_QUEUE_H

#include<iostream>
#include<string>
using namespace std;




template<typename T>
class MyQueue
{
public:
	MyQueue();
	T pop_front();
	void push_back(T item);
	bool isEmpty() const;
	void print() const;
	
private:
	//const int MAXI = 100;<----that didn't work just place the 
	//literal value 100	
	T queue[100];
	int front;
	int back;
	
};//End 

//Create an empty queue
template<typename T>
MyQueue<T>::MyQueue()
{
	front = 0;
	back = 0;
}
//remove and return the first item in the queue
template<typename T>
T MyQueue<T>:: pop_front()
{
	T contain = queue[100];
	front++;
	front = front % 100;
	return contain;
}
//add item to the end of queue
template<typename T>
void MyQueue<T>::push_back(T item)
{
	
	queue[back] = item;
	back++;
	back = back % 100;
	
}
//returns true if the queue is empty
template<typename T>
bool MyQueue<T>::isEmpty() const
{
	if(front == back)
		return true;
	else
		return false;
	
}
//print put the elements of the queue from front to back speratied by one space
template<typename T>
void MyQueue<T>::print()const
{
	for(int i = front; i != back; i = (i + 1) % 100)
		cout << queue[i] << " " << "\n";
	
}//end print()
#endif







#ifndef MYSTACK_H
#define MYSTACK_H

#include <iostream>
//#include <stack>
using namespace std;

template <typename T>
class MyStack
{
public:
    MyStack();
	T pop(); 
    void push(T item);
    bool isEmpty() const;
private:
	int top;
	T* items;
    int MAX;
	
	
};

template <typename T>
MyStack<T>::MyStack()
{
	int size=100;
	MAX=size;
	top=-1;
	items=new T[MAX];
	
}
template <typename T>
T MyStack<T>::pop()
{
	if(isEmpty())
	{
		cout<< "" <<endl;
		// exit(1);
	}
	return items[top--];
}

template <typename T>
void MyStack<T>::push(T item)
{
    if(!isEmpty())
    {
		cout<<"" <<endl;
		// exit(1);
    }
	
    items[++top] = item;
	
}
template <typename T>
bool MyStack<T>::isEmpty() const
{ 
    return top == -1;
}
#endif







Is This A Good Question/Topic? 0
  • +

Replies To: template stacks and queues

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: template stacks and queues

Posted 12 May 2011 - 10:29 AM

Where are these classes being used? Its hard to see what might be going on if we can't see what methods are being called and how.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: template stacks and queues

Posted 12 May 2011 - 10:29 AM

The private member variables in MyQueue, should probably be pointers to type T:
T *front, *back;

You have them just as integers. Front is supposed to point to the front of the queue, and back to the back of the queue. Also, could you post the main() part of your program?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: template stacks and queues

Posted 12 May 2011 - 10:34 AM

:/ I think front and back are correct as they are used as index's into the array "queue".
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: template stacks and queues

Posted 12 May 2011 - 10:36 AM

You're obviously correct, but what's the point of writing a queue or stack, to just have it as a static array?
Was This Post Helpful? 0
  • +
  • -

#6 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 171
  • Joined: 05-March 09

Re: template stacks and queues

Posted 12 May 2011 - 10:45 AM

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;

#include "mystack.h"
#include "myqueue.h"

#define INFILE "assignment_seven_input.txt"

static void add(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 + op2);
}

static void subtract(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 - op2);
}

static void multiply(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 * op2);
}

static void divide(MyStack<double> &s)
{
	double op2 = s.pop();
	double op1 = s.pop();
	s.push(op1 / op2);
}

int main()
{
	ifstream inFile;
	inFile.open(INFILE);
	if(!inFile.is_open())
	{
		cout << "\nCould not open file " << INFILE << endl;
		cout << "Place file " << INFILE << " in same directory as a.out\n" << endl;
		return 1;
	}
	
	MyStack<double> stack;
	MyStack<char> charStack;
	MyQueue<string> input;
	
	cout << "Input is: ";
	string token;
	inFile >> token;
	
	while(token != "EOF")
	{
		cout << token << " ";
		input.push_back(token);
		for(int ii = 0; ii < token.length(); ++ii) charStack.push(token[ii]);
		inFile >> token;
	}
	
	inFile.close();
	cout << endl;
	
	cout << "Contents of input queue: ";
	input.print();
	
	while(!input.isEmpty())
	{
		token = input.pop_front();
		if(token == "+")
		{
			add(stack);
		}
		else if(token == "-")
		{
			subtract(stack);
		}
		else if(token == "*")
		{
			multiply(stack);
		}
		else if(token == "/")
		{
			divide(stack);
		}
		else
		{
			const char *n = token.data();
		    char *endPtr = NULL;
			double value = strtod(n, &endPtr);
		    stack.push(value);
		}
	} // end while reading input queue
	
	double result = stack.pop();
	
	cout << "Result is " << result << endl;
	
	cout << "Input reversed is: ";
	while(!charStack.isEmpty())
	{
		cout << charStack.pop() << " ";
	}
	cout << endl;
	
	return 0;
	
} // end main()


Was This Post Helpful? 0
  • +
  • -

#7 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 171
  • Joined: 05-March 09

Re: template stacks and queues

Posted 12 May 2011 - 11:03 AM

If i change the front and back i get way more errors because of the ways they're being called
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: template stacks and queues

Posted 12 May 2011 - 11:52 AM

Ya, like NickDMax said, you're using front & back as indexes so they don't need to be pointers.
Was This Post Helpful? 0
  • +
  • -

#9 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,376
  • Joined: 31-December 10

Re: template stacks and queues

Posted 12 May 2011 - 11:58 AM

One thing I noticed is line #94,
const char* n = token.data();

Try this and see if it helps:
const char* n = token.c_str();

The reason for this is that the string::data() function returns a pointer to an array of characters with the same content as the string, but doesn't append the NULL('\0') character to the string. For C-style strings, you need the NULL character because there will be no way to tell where the end of the string is.
Was This Post Helpful? 0
  • +
  • -

#10 custurd122000  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 171
  • Joined: 05-March 09

Re: template stacks and queues

Posted 12 May 2011 - 12:08 PM

Well im not allowed to change the main the professor wrote that and I have to write the stack and queues so that the main works :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1