Thread: I need help :(

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    6

    Unhappy I need help :(

    you see, i have this program implementing stack and queue. my problem is that when my program executes dequeue(i used leave), the dequeued number should immediately transfer and be displayed in my stack(airplaine). my program does the dequeue and push just fine but the display is late.

    for example: if i dequeue 2 & 3, then the moment that 2 is dequeued, it should be displayed in the airplaine list(stack). But what happens is that, 2 is only displayed when 3 is already dequeued.

    i also need the program to end ONLY when any key is pressed on the keyboard.


    main program:
    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<conio.h>
    #include<math.h>
    #include<time.h>
    #include<windows.h>
    #include "gotoxy.h"
    #include "queue.h"
    #include "stack.h"
    
    int rand(void);
    
    main(){
    int num1,num2,num3,temp=0;
    Queue Q;
    Stack S;
    do{
    
        num1=rand()%100+1;
        num2=rand()%100+1;
        num3=rand()%100+1;
    
        if((num1%2)==1){
             Q.Join(num2);
             clrscr();
             cout<<"Customer Representative:"<<endl;
             cout<<"------------------------"<<endl;
             cout<<"Now Serving: ";
             gotoxy(30,2);cout<<"Airplane: ";
             Q.Display();
             S.Display();                      
        }
        
        if((num3%2)==1){
             S.Push(Q.Leave());
             clrscr();
              cout<<"Customer Representative:"<<endl;
              cout<<"------------------------"<<endl;
              cout<<"Now Serving: ";
              gotoxy(30,2);cout<<"Airplane: ";
              Q.Display();
                    S.Display();
        }
    
        delay(1000);
        
    }while(true);
    
    getch();
    }
    queue.h:
    Code:
    #include<iostream.h>
    
    class Queue
    {
          private:
             int Q[100];
             int front,rear;
          public:
             Queue(void){front=rear=-1;}
             void Join(int item);
             int Leave(void);
             int EmptyQ(void);
             void Display(void);
    };
    
    void Queue::Join(int item)
    {
         if(rear==99)
             rear=0;
         else
             rear++;
         Q[rear]=item;
    }
    
    int Queue::Leave(void)
    {
        int temp=Q[front];
        front ++;
        return temp;
    }
    
    int Queue::EmptyQ(void)
    {
        if(rear<0 || rear==front)
            return 1;
        else
            return 0;
    }
    
    void Queue::Display(void)
    {   int n=2;
        for(int z=front;z<rear+1;z++){
                gotoxy(14,n);cout<<Q[z];
                n++;
        }
    }
    stack.h:
    Code:
    #include <iostream.h>
    
    class Stack
    {
    	private:
    		int S[50];
    		int top;
    	public:
    		Stack(void);
    		void Push(int elem);
    		int Pop(void);
    		int Top(void);
    		int Empty(void);
    		void Display(void);
    };
    
    Stack::Stack(void)
    {
    	top=-1;
    }
    
    void Stack::Push(int elem)
    {
    	top++;
    	S[top]=elem;
    }
    
    int Stack::Pop(void)
    {
    	int temp = S[top];
    	top--;
    	return temp;
    }
    
    int Stack::Top(void)
    {
    	return S[top];
    }
    
    int Stack::Empty(void)
    {
        if(top<0)
            return 1;
        else
            return 0;
    }
    
    void Stack::Display(void)
    {
        int m=2;
        for(int i=1;i<top;i++){
                gotoxy(40,m);cout<<S[i];
                m++;
        }
    }
    i use DevC++ which does not have its own gotoxy, clrscr, and delay function, so i made my own header file.

    gotoxy.h
    Code:
    void gotoxy(int x, int y)
    {
    HANDLE hConsoleOutput;
    COORD dwCursorPosition;
    cout.flush();
    dwCursorPosition.X = x;
    dwCursorPosition.Y = y;
    hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }
    
    void clrscr()
    {
    system("cls");
    } 
    
    void delay( clock_t wait )
    {
    clock_t goal;
    goal = wait + clock();
    while( goal > clock() )
    ;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > my program does the dequeue and push just fine but the display is late.
    You need to complete the sequence of cout statements with a
    cout.flush();

    Are you a C or C++ programmer?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    6
    what does the cout.flush() do?

    I do a little of both. It depends on what program i need to make.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SourceForge.net: Implicit main - cpwiki is not allowed in C++.
    iostream.h is deprecated, old header. Use iostream.
    Also use the C++ headers: cstdio, cmath, ctime.
    Furthermore, what compiler do you use?

    cout.flush() flushes the output buffer and makes sure everything you've printed appear on the screen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what does the cout.flush() do?
    I would have hoped you would have looked it up in the manual in a few minutes, rather than waiting hours for someone to respond on a message board.

    If you're going to post "what does x do" every time someone mentions a new x, it's going to get old really fast.


    > I do a little of both. It depends on what program i need to make.
    This never turns out well in the long run - just sayin, that's all...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This appears to actually be about C++, so I am moving the thread accordingly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed