Thread: how to clear screen after short pause

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    how to clear screen after short pause

    When I run this program the numbers are supposed to display for a short time before screen clears. However the 'short pause' seems to happen before the numbers are displayed, then screen clears as soon as they do display, i.e. too fast for eye to see.

    Also I'm using a 'for' loop to clear the screen because 'system("clear");' doesnt seem to work in Xcode.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
    	int iNum1 = 0;
    	int iNum2 = 0;
    	int iNum3 = 0;
    	int iAns1 = 0;
    	int iAns2 = 0;
    	int iAns3 = 0;
    	int iCurrentTime = 0;
    	int iElapsedTime = 0;
    	int iRand = 0;
    	int iCount = 0;
    	char cYesorNo = '\0';
    	int x = 0;
    	
    	srand(time(NULL));
    	
    	printf("\nPlay a Game YES (y) or NO (n): \n");
    	scanf("%c", &cYesorNo);
    	
    	if (cYesorNo == 'Y' || cYesorNo == 'y') { //if1
    		
    		iNum1 = rand() % 100 + 1;
    		iNum2 = rand() % 100 + 1;
    		iNum3 = rand() % 100 + 1;
    		
    		printf("\nConcentrate on these 3 numbers:\n");
    		printf("%d %d %d", iNum1, iNum2, iNum3);
    		
    		iCurrentTime = time(NULL); //
    		
    		do { // do1
    			iElapsedTime = time(NULL);
    		} while ((iElapsedTime - iCurrentTime) < 3); // do1
    		
    		//system ("clear");
    		for (x = 0; x < 100; x++) {
    			printf("\n");
    		}
    		
    		printf("\nEnter the #'s, with a space between each one: \n");
    		scanf("%d %d %d", &iAns1, &iAns2, &iAns3);
    		
    		if (iNum1 == iAns1 && iNum2 == iAns2 && iNum3 == iAns3) { // if 2
    			printf("\n*** CONGRATULATIONS ***\n");
    					}// if2
    		else {//else1
    			printf("\nSorry, the correct numbers were %d %d %d", iNum1, iNum2, iNum3);
    			}//else2
    
    	}//if1
    	return 0;
    }//main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > printf("%d %d %d", iNum1, iNum2, iNum3);
    Add
    fflush( stdout );
    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
    Apr 2010
    Posts
    51
    cheers Salem

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    So fflush(stdout); - clears the output buffer and ensures the output is printed to the screen asap?

    Now the time countdown works (output stays on screen for about 3 seconds) before the "clear" command starts.

    My question are,
    1. before I added the fflush, why did the output wait a few seconds before appearing on screen (I have never seen this before with a simple printf command)?
    2. Why didn't the time delay do..while loop work before fflush was added above it?

    Thanks for the help.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dunsta View Post
    So fflush(stdout); - clears the output buffer and ensures the output is printed to the screen asap?

    Now the time countdown works (output stays on screen for about 3 seconds) before the "clear" command starts.

    My question are,
    1. before I added the fflush, why did the output wait a few seconds before appearing on screen (I have never seen this before with a simple printf command)?
    2. Why didn't the time delay do..while loop work before fflush was added above it?

    Thanks for the help.
    Just as what you type at the keyboard doesn't get passed to the system until you press enter-key, what the program types to the console doesn't get passed to the system until your program "presses enter-key" by printing a \n character or by doing fflush. (EDIT: Of course, this is a very large over-simplification, but it's the general idea.)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > 1. before I added the fflush, why did the output wait a few seconds before appearing on screen
    It was waiting for the first of the \n's you print (which is another cause for flushing the buffer).

    For a buffered stream, output is flushed
    - when the buffer is full
    - when you print a \n
    - when you call fflush()
    - when you close the stream
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And your compiler should have options to switch between buffered (will wait), and unbuffered (prints right away).

    As a student, you'll enjoy the unbuffered style, much more. Good to understand the buffered type, however.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I retyped the program and can't figure out why, after the user enters the 3 numbers and hits (enter) key, I get a windows error message pop-up

    "Concentration game.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
    	int x = 0;
    	int iNum1 = 0;
    	int iNum2 = 0;
    	int iNum3 = 0;
    	int iAns1 = 0;
    	int iAns2 = 0;
    	int iAns3 = 0;
    	int iCurrentTime = 0;
    	int iElapsedTime = 0;
    	char YesOrNo = '\0';
    
    	srand((unsigned)time(NULL));
    
    		printf("\t***********************************\n");
    		printf("\t*     A GAME OF CONCENTRATION     *\n");
    		printf("\t***********************************\n");
    		printf("\n\tWould you like to play a game ('Y' or 'N') ?: ");
    		scanf("%c", &YesOrNo);
    
    		if (YesOrNo == 'y' || YesOrNo == 'Y'){//if1
    						
    				
    				iNum1 = ((unsigned)rand()) %100 + 1;
    				iNum2 = ((unsigned)rand()) %100 + 1;
    				iNum3 = ((unsigned)rand()) %100 + 1;
    				printf("Watch the 3 numbers closely.\n");
    				printf("\n\n%d %d %d\n", iNum1, iNum2, iNum3);
    				fflush(stdout);
    
    				iCurrentTime = ((unsigned)time(NULL));
    
    				do{
    					iElapsedTime = ((unsigned)time(NULL));
    				}while((iElapsedTime - iCurrentTime) <2);
    
    				system("cls");
    				//for(x=0;x<100;x++){
    				//	printf("\n");
    				//}
    
    				printf("Enter the 3 numbers with a space in between: \n");
    				scanf("%d %d %d", &iAns1, iAns2, iAns3);
    				if (iAns1 == iNum1 && iAns2 == iNum2 && iAns3 == iNum3){//if2
    					printf("\n\n\t\tCONGRATULATIONS!\n");
    				}//if2
    
    				else{	//else2
    					printf("\nSorry, the correct answers were %d %d %d", iNum1, iNum2, iNum3);
    				}//else2
    
    		
    		}//if1
    		/*else {
    			printf("GOODBYE");
    		}*/
    
    		return 0;
    
    }//main
    I compared it to my code in post 1 and can't find why code in post 1 works, but the new one doesn't??

    Thanks for any help.

  9. #9
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    scanf("%d %d %d", &iAns1, iAns2, iAns3);
    Forgot the &'s.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    oh man!



    Thanks


  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    now after I have added a loop, the program runs once, but then after the loop, runs twice, then waits for user input?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
    	int x = 0;
    	int iNum1 = 0;
    	int iNum2 = 0;
    	int iNum3 = 0;
    	int iAns1 = 0;
    	int iAns2 = 0;
    	int iAns3 = 0;
    	int iCurrentTime = 0;
    	int iElapsedTime = 0;
    	char YesOrNo = '\0';
    
    	srand((unsigned)time(NULL));
    
    	
    		
    	do {
    		printf("\n\t***********************************\n");
    		printf("\t*     A GAME OF CONCENTRATION     *\n");
    		printf("\t***********************************\n");
    		printf("\n\tWould you like to play a game ('Y' or 'N') ?: ");
    		scanf("%c", &YesOrNo);
    
    		if (YesOrNo == 'y' || YesOrNo == 'Y'){//if1
    						
    				
    				iNum1 = ((unsigned)rand()) %100 + 1;
    				iNum2 = ((unsigned)rand()) %100 + 1;
    				iNum3 = ((unsigned)rand()) %100 + 1;
    				printf("Watch the 3 numbers closely.\n");
    				printf("\n\n%d %d %d\n", iNum1, iNum2, iNum3);
    				fflush(stdout);
    
    				iCurrentTime = ((unsigned)time(NULL));
    
    				do{
    					iElapsedTime = ((unsigned)time(NULL));
    				}while((iElapsedTime - iCurrentTime) <2);
    
    				system("cls");
    				//for(x=0;x<100;x++){
    				//	printf("\n");
    				//}
    
    				printf("Enter the 3 numbers with a space in between: \n");
    				scanf("%d %d %d", &iAns1, &iAns2, &iAns3);
    				if (iAns1 == iNum1 && iAns2 == iNum2 && iAns3 == iNum3){//if2
    					printf("\n\n\t\tCONGRATULATIONS!\n");
    				}//if2
    
    				else{	//else2
    					printf("\nSorry, the correct answers were %d %d %d\n", iNum1, iNum2, iNum3);
    				}//else2
    
    		
    		}//if1
    
    		else if  (YesOrNo == 'n' || YesOrNo == 'n'){
    				
    					printf("\nGOODBYE\n");
    					break;
    						}
    	
    	}while(1);
    
    		return 0;
    
    }//main

  12. #12
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    scanf when used with the integer format specifier doesn't deal with the newline character at the end of the input. So when you use scanf with the character format specifier, it is picking up the newline, and because your if statements don't touch this it will loop around and go back to the beginning.

    STDIN pitfalls

    Someone posted this in a thread yesterday (can't remember who), and it seems to cover your problem.

    EDIT: Oh, looking at the website, I think it was MK27. Sorry, didn't realise it was your own site, I'll refrain from posting it next time.
    Last edited by DeadPlanet; 04-26-2010 at 02:23 AM.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I tried to understand the article as best as I could, but some of it is a bit over my head at the moment. The code I thought might help, didn't. I assume I am using it wrong, or I'm way off. But I still get the "would you like ??" offered twice.
    Code:
    printf("\n\tWould you like to play a game ('Y' or 'N') ?: ");
    		scanf("%c", &YesOrNo);
    		//fgets(YesOrNo, 2, stdin);
    		while (scrap != '\n') scrap = getchar();

  14. #14
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    	printf("Enter the 3 numbers with a space in between: \n");
    	scanf("%d %d %d", &iAns1, &iAns2, &iAns3);
    Try here.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    I entered the scrap newline loop after the user enters the 3 answers, but sometimes the "Would you like..." appears only once on the first iteration, but then runs through twice after selecting a new game??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(){
    	int scrap = 0;
    	int iNum1 = 0;
    	int iNum2 = 0;
    	int iNum3 = 0;
    	int iAns1 = 0;
    	int iAns2 = 0;
    	int iAns3 = 0;
    	int iCurrentTime = 0;
    	int iElapsedTime = 0;
    	char YesOrNo = '\0';
    	//char YesOrNo[2];
    	
    	srand((unsigned)time(NULL));
    	
    	
    	
    	do {
    		printf("\n\t***********************************\n");
    		printf("\t*     A GAME OF CONCENTRATION     *\n");
    		printf("\t***********************************\n");
    		printf("\n\tWould you like to play a game ('Y' or 'N') ?: ");
    		scanf("%c", &YesOrNo);
    		//fgets(YesOrNo, 2, stdin);
    		while (scrap != '\n') scrap = getchar();
    		
    		if (YesOrNo == 'y' || YesOrNo == 'Y'){//if1
    			
    			
    			iNum1 = ((unsigned)rand()) %100 + 1;
    			iNum2 = ((unsigned)rand()) %100 + 1;
    			iNum3 = ((unsigned)rand()) %100 + 1;
    			printf("Watch the 3 numbers closely.\n");
    			printf("\n\n%d %d %d\n", iNum1, iNum2, iNum3);
    			fflush(stdout);
    			
    			iCurrentTime = ((unsigned)time(NULL));
    			
    			do{
    				iElapsedTime = ((unsigned)time(NULL));
    			}while((iElapsedTime - iCurrentTime) <2);
    			
    			system("cls");
    			//for(x=0;x<100;x++){
    			//	printf("\n");
    			//}
    			
    			printf("Enter the 3 numbers with a space in between: \n");
    			scanf("%d %d %d", &iAns1, &iAns2, &iAns3);
    			
    			while(scrap != '\n') scrap = getchar();
    			
    			if (iAns1 == iNum1 && iAns2 == iNum2 && iAns3 == iNum3){//if2
    				printf("\n\n\t\tCONGRATULATIONS!\n");
    				
    			}//if2
    			
    			else{	//else2
    				printf("\nSorry, the correct answers were %d %d %d\n", iNum1, iNum2, iNum3);
    			
    			}//else2
    			
    			
    		}//if1
    		
    		else if  (YesOrNo == 'n' || YesOrNo == 'n'){
    			
    			printf("\nGOODBYE\n");
    			break;
    		}
    		
    	}while(1);
    	
    	return 0;
    	
    }//main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a clear screen...
    By Finchie_88 in forum C++ Programming
    Replies: 13
    Last Post: 09-03-2004, 05:38 PM
  2. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 08:36 AM
  3. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 05:14 AM
  4. How to clear the screen?
    By Zopyrus in forum C++ Programming
    Replies: 8
    Last Post: 11-07-2003, 10:20 PM
  5. Clear Screen
    By KneeGrow in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2003, 10:17 PM