Never ending loop

This is a discussion on Never ending loop within the C++ Programming forums, part of the General Programming Boards category; I'm writing an infix to postfix program. All has been slowly but surely going well, but now I'm actually running ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    54

    Never ending loop

    I'm writing an infix to postfix program. All has been slowly but surely going well, but now I'm actually running the program after getting rid of all warning and errors the compiler found, and there's a problem with a loop somewhere that I can't figure out.

    I'm pretty sure the problem lies in my queue class, as what I'm getting is endless lines of "Queue is full". But I can't spot what's wrong, although it's probably something simple.

    Code:
    Queue::Queue() {
    	Front = Rear = 0;
    }//end constructor
    
    bool Queue::Empty() {
    	return Front == Rear == 0;
    }//end Empty
    
    void Queue::Enqueue(int element) {
    	if(Rear+1 == Front || (Rear+1 == maxqueue && !Front)) {
    		cout << "Queue is full\n";
    		return;
    	}//if
    	Rear++;
    	if(Rear == maxqueue)
    		Rear = 0; //circle
    	Cue[Rear] = element;
    }//end Enqueue
    
    int Queue::Dequeue() {
    	if (Front == Rear) {
    		cout << "Queue is empty\n";
    		return 0;
    	}//if
    	Front++;
    	if (Front == maxqueue)
    		Front = 0;
    	return Cue[Front];
    }//end Dequeue
    There could also be a problem in this function, but I think it's ok.

    Code:
    void infix_to_postfix(string infix) {
           Stack stak;
    	   Queue cue;
           for (int i=0; i<infix.size(); i++){
               
               if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '^' ){
                   while (!stak.Empty() && order_ops(stak.Top()) <= order_ops(infix[i])){
                         cue.Enqueue(stak.Top());
                         stak.Pop();
                   }//while
                   stak.Push(infix[i]);
               }//if
               else if(infix[i] == '('){
                    stak.Push(infix[i]);
               }//else if
               else if(infix[i] == ')'){
                    while (stak.Top() != '('){
                          cue.Enqueue(stak.Top());
                          stak.Pop();
                    }//while
                    stak.Pop();
               }//else if
               else{
                   cue.Enqueue(infix[i]);
               }//else
               
           } //for
           
           while (!stak.Empty()){
                 cue.Enqueue(stak.Top());
                 stak.Pop();
           }
           // Print queue (postfix) to screen
           if (!cue.Empty())
              cout << "Postfix : " << endl;
           while(!cue.Empty()){
                cout << cue.Dequeue();
                //cue.Dequeue();
           }//while
           cout << endl << endl;
    }//end infix_to_postfix

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    Quote Originally Posted by mikeman
    I'm pretty sure the problem lies in my queue class, as what I'm getting is endless lines of "Queue is full". But I can't spot what's wrong, although it's probably something simple.
    So what you should do is test your queue class separately. Alternatively, you turn your queue class into a wrapper for std::queue<int>, thus you can be pretty certain that if a bug is detected, then it lies with your infix to postfix conversion implementation, not with the queue implementation. Later, if necessary, you can always switch the implementation to your own queue implementation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
    bool Queue::Empty() {
    	return Front == Rear == 0;
    }//end Empty
    I don't think this is what you actually wanted to do. I'm guessing you're after something more like return Front==0 && Rear==0;
    or return Front==Rear;


    I would like to second what laserlight said about testing everything separately. You need to make sure that all of your pieces are working right before you try to put them together.
    Last edited by NeonBlack; 02-15-2010 at 11:22 AM. Reason: addition
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    54
    Got started on breaking it down but have to break till later, have a class to go to.

    One issue I'm seeing and I think is due to type issues:
    I load a char array with "ABCD" and load the first element into the stack and queue, and my output is "65" which would be ASCII Decimal code. Is this indeed caused by the fact that my return type is int while the loaded item, "A" in this instance, is a char? Or am I totally off base with this thought?

    I need to output the actual character, as I'm going to have to evaluate the postfix when it's correctly translated, and well, ASCII does me no good! If this is what's going on, should I change my return types for things like push and pop / enqueue and dequeue to char instead of int or void?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21