Thread: Keeping window open

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    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++);
    }

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    Code:
    fflush ( stdin );
    ...just kidding, don't do that. 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();
    My best code is written with the delete key.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    or put a while(1); at the end, and hit control-c when done.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    thanks! that worked...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open URL in new window
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2006, 02:17 AM
  2. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  3. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Keeping the console window open
    By Adock in forum C Programming
    Replies: 3
    Last Post: 01-31-2002, 05:15 AM