-
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() )
;
}
-
> 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?
-
what does the cout.flush() do?
I do a little of both. It depends on what program i need to make.
-
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.
-
> 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...
-
This appears to actually be about C++, so I am moving the thread accordingly.