Thread: Why is system("clear") executing prematurely?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Why is system("clear") executing prematurely?

    In input(), system("clear") executes first in a while loop, and then add(); is called... however, before add(); finishes and returns to input(), system("clear"); is called again and executes in the middle of add();

    Code:
    	while (continu==1){
    		system("clear");
    		printf("\n\nEnter Command: ");
    			fgets(input, sizeof(input), stdin);
        				if ((p = strchr(input, '\n')) != NULL)
          				*p = '\0';
    			if (strcmp(input, "a")==0){add();}
    	}
    Here is add();
    Code:
    void add(){
    char input[250];
    char *p;
    
    		List *new=(List *)malloc(sizeof(List));
    
    
    
    		printf("\n\nEnter Name: ");
    			fgets(input, 250, stdin);
        				if ((p = strchr(input, '\n')) != NULL)
          				*p = '\0';
    			strcpy(new->name, input);
    			printf("Name: %s", new->name);
    
    		printf("\n\nEnter Number: ");
    			fgets(input, sizeof(input), stdin);
        				if ((p = strchr(input, '\n')) != NULL)
          				*p = '\0';
    				strcpy(new->number, input);       <--------- Why would system("clear") execute here??????
    				printf("Number: %s", new->number);
    }
    When I run the program, system("clear"); executes a second time, right before the final line of add(); such that the screen is cleared prematurely, and then the final printf statement is executed, followed by the "Enter Command" from input();
    Last edited by Adam Rinkleff; 07-13-2011 at 01:31 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    It should look like this:

    ---------------Top of the Screen------------------


    Enter Command:


    Instead I get:

    ---------------Top of the Screen------------------
    Number: 123456789

    Enter Command:
    Last edited by Adam Rinkleff; 07-13-2011 at 01:41 AM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    printf does not guarantee a write unless you have a newline or an fflush call after it.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Thanks! The following seems to work just fine:

    Code:
    printf("Number: %s\n", new->number);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to: "Clear screen" in C program
    By matthewlane in forum C Programming
    Replies: 3
    Last Post: 11-03-2010, 11:44 PM
  2. Using the system("clear") command in Vista
    By maverick95 in forum C Programming
    Replies: 15
    Last Post: 08-13-2008, 08:58 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Replies: 3
    Last Post: 10-11-2001, 07:43 AM