MY PROGRAM CRAHES AND I DON'T KNOW WHY?

MY PROGRAM CRAHES AND I DON'T KNOW WHY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
using namespace std;

template<class Type>
class queueType
{
public:
       queueType(int queueSize);
       queueType(const queueType<Type>& otherQueue);
       ~queueType();
       Type& operator=(const queueType<Type>& otherQueue);
       void initializeQueue();
       bool isEmpty(){return (count == 0);};
       bool isFull(){return (count == queueMaxSize);};
       void addQueue(const Type& newItem);
       void removeFirst(queueType<Type>& Q, const Type& x);
       void queueSize(){return count;};
       Type back();
       Type front();
private:
        void copyQueue(const queueType<Type>& otherQueue);
        int count, queueMaxSize, queueFront, queueRear;
        Type* list;
};

int main()
{
    queueType<int> Queue(10);
    
    Queue.addQueue(9);
    Queue.addQueue(7);
    Queue.addQueue(8);
    Queue.addQueue(1);
    Queue.addQueue(3);
    Queue.addQueue(4);
    
    
    
    
    system("PAUSE");
    
    return 0;   
}

template<class Type>
queueType<Type>::queueType(int queueSize)
       {
            if(queueSize <= 0){
               cout << "Can't work with an empty queue"
                    << "creating a queue of 100 items by default"<<endl;
               queueMaxSize = 100;
            }
            else{
               queueMaxSize = queueSize;
               count = 0;
               queueFront = 0;
               queueRear = queueMaxSize-1;
               list = new Type[queueMaxSize];                          
       }
}
       
template<class Type>
void queueType<Type>::copyQueue(const queueType<Type>& otherQueue)
        {
             queueFront = otherQueue.queueFront;
             queueRear = otherQueue.queueRear;
             count = otherQueue.count;
             queueMaxSize = otherQueue.queueMaxSize;
             
             if(this != &otherQueue)
               delete [] list;
               
             list = new Type[queueMaxSize];
             
             for(int i=0; i<count; i++)
                 list[i] = otherQueue.list[i];    
        }
       
template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
    copyQueue(otherQueue);
}

template<class Type>
queueType<Type>::~queueType(){delete [] list;}

template<class Type>
Type& queueType<Type>::operator=(const queueType<Type>& otherQueue)
       {
             if(this != &otherQueue)
                copyQueue(otherQueue);
                return *this;
       }
       
template<class Type>
void queueType<Type>::initializeQueue()
       {
            queueFront = 0;
            queueRear = queueMaxSize-1;
            count = 0;
       }
       
template<class Type>
void queueType<Type>::addQueue(const Type& newItem)
       {
            if(!isFull()){
              queueRear = (queueRear+1)%queueRear;
              count++;
              list[queueRear] = newItem;
            }
            else
              cout << "Cannot add to a full queue" <<endl;              
            
       }
       
template<class Type>
Type queueType<Type>::back(){return list[queueRear];}

template<class Type>
Type queueType<Type>::front(){return list[queueFront];}

queueRear = (queueRear+1)%queueRear;

When queueRear is zero you're trying to calculate 1%0. Don't.

Also, please learn to debug. Finding the problem took literally thirty seconds. Here's the output that I used to find the problem:


(gdb) run
Starting program: /home/j/badCode/a.out 

Program received signal SIGFPE, Arithmetic exception.
0x0000000000400c5b in queueType<int>::addQueue (this=0x7fffffffe2c0, 
    newItem=@0x7fffffffe2e0) at 303.cpp:108
108	              queueRear = (queueRear+1)%queueRear;
(gdb) print queueRear
$1 = 0

Thank you Moschops. I made a silly mistake there. the statement should have been as follows.queueRear = (queueRear+1)%queueMaxSize;
Topic archived. No new replies allowed.