-
Keeping window open
I have written a very simple app with Visual C++, and when I compile the program, it runs fine. However, the window closes right away when I'd like for it to stay open until I hit a key....
what's the correct function to keep the window open?
I have attached the code of the program.
Code:
#include <stdio.h>
#include <stdlib.h>
struct process
{
int memorySize;
int processNumber;
bool isExecuting;
bool isOverflow;
bool isStaged;
bool isDone;
};
typedef struct process PROCESS;
void initNstart(PROCESS[], int, int);
main()
{
int totalExecSpace=64;
int totalProcesses=5;
PROCESS pr[5];
initNstart(pr, totalProcesses, totalExecSpace);
return 0;
}
void initNstart(PROCESS proc[], int size, int totalMem)
{
void start(PROCESS[], int, int);
int i;
for (i=0; i<size; i++)
{
proc[i].processNumber=i+1;
proc[i].isDone=false;
proc[i].isStaged=false;
proc[i].isExecuting=false;
proc[i].isOverflow=false;
printf("Enter Process Memory Size (not bigger than 64) For Process # %d\t", proc[i].processNumber);
scanf("%d", &proc[i].memorySize);
if(proc[i].memorySize>64)
{
printf("\n!You have entered size larger than size of Execute Q (64)! Size of process# %d is set to 30 MB\n", proc[i].processNumber);
proc[i].memorySize=30;
}
}
start(proc, size, totalMem);
}
void start(PROCESS proc[], int arraySize, int ExecMemSize)
{
void PushExec(PROCESS[], int, int);
void PushOver(PROCESS[], int);
void PushStag(PROCESS[], int);
void runTime(void);
bool allDone(PROCESS[], int);
do{
PushStag(proc, arraySize);
PushExec(proc, arraySize, ExecMemSize);
PushOver(proc, arraySize);
runTime();
}while(!allDone(proc, arraySize));
}
bool allDone(PROCESS proc[], int size)
{
int i;
int d=size;
for(i=0; i<size; i++)
{
if(proc[i].isDone==true)
{
d-=1;
}
}
if(d==0)
return true;
else
return false;
}
void PushStag(PROCESS proc[], int size)
{
int q;
for(q=0; q<size; q++)
{
if(proc[q].isDone!=true)
{
proc[q].isStaged=true;
printf("\nProcess Number %d is in Staged Q with size %d\n", proc[q].processNumber, proc[q].memorySize);
}
}
}
void PushExec(PROCESS proc[], int size, int memLeft)
{
int q;
int sizeLeft=0;
for(q=0; q<size; q++)
{
if(proc[q].isDone!=true)
{
if(memLeft>=proc[q].memorySize)
{
proc[q].isExecuting=true;
proc[q].isDone=true;
printf("\nTOTAL MEMORY LEFT = %d\nProcess Number %d is in EXEC Q with size %d\n", memLeft, proc[q].processNumber, proc[q].memorySize);
memLeft-=proc[q].memorySize;
}
}
}
}
void PushOver(PROCESS proc[], int size)
{
int q;
for(q=0; q<size; q++)
{
if(proc[q].isDone!=true)
{
if(proc[q].isExecuting==false && proc[q].isStaged==true)
{
proc[q].isOverflow=true;
printf("Process Number %d is in OVERFLOW Q with size %d", proc[q].processNumber, proc[q].memorySize);
}
}
}
}
void runTime(void)
{
float k;
for(k=0; k<10000000; k++);
}
-
Just before the return o; add getchar();
This will wait for the user to hit return before the program exits and the window closes. You may add a prompt before the getchar if you wish.
-
The program runs fine if I compile it with Visual C++ with the new code - but it still closes immediately if I run the executable.
-
>but it still closes immediately if I run the executable
Then you still have a newline sitting in stdin. To get rid of it, use
...just kidding, don't do that. :D A better way is to avoid scanf altogether using fgets and sscanf, but you can also take the easy way out by replacing that call to getchar with:
Code:
/* Clear out stdin */
while ( getchar() != '\n' )
;
/* Now wait for user input */
getchar();
-
or put a while(1); at the end, and hit control-c when done.
-