Thread: Countdown Program not displaying anything in terminal.

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    16

    Countdown Program not displaying anything in terminal.

    Im having strange issues. I got my code to display the countdown all at once but once i added sleep(1) it no longer worked. (had to add unistd.h due to conflicting types for sleep) when i tried to go back to just having all the number display nothing would still show up. Any ideas? I still need to test to see if P and U still work for pausing and resuming respectively.

    Code:
    #include <stdio.h>
    #include <termios.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    
    int main(int argc, const char * argv[])
    {
        //create terminal interface with termio
        struct termios oldTerm;
        struct termios newTerm;
        //Get the attributes of the terminal
        tcgetattr(0, &oldTerm);
        //If wrong number of arguments
        if(argc != 2)
        {
            printf("Wrong number of arguments. ");
        }
        else
        {
            //get the Number from argv
            int startNum = atoi(argv[1]);
            printf("Initiating Countdown.");
            newTerm.c_iflag &= ~IXOFF;
            //Set P to pause countdown
            newTerm.c_cc[VSTOP] = 'P';
            //set U to resume countdown
            newTerm.c_cc[VSTART] = 'U';
            //set terminal attributes to take effect immediatly
            tcsetattr(1, TCSANOW, &newTerm);
            for(; startNum > 0; startNum--)
            {
                printf("%d", startNum);
                //sleep(1);
            }
        }
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You probably need to add a
    fflush(stdout);
    every time you call printf (or other output function) which doesn't end with a newline.

    Or you make stdout an unbuffered 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.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    I looked it up and attempted to implement it but i still had no output. Only way i got output was at the end of the program all at once. Here is the code ive added. I've commented out the things ive tried but didnt work.

    Code:
    tcsetattr(1, TCSANOW, &newTerm);
            //create buffer for int to char conversion
            //char numToWord[25];
            setvbuf(stdout, (char *)NULL, _IONBF, 0);
            for(; startNum > 0; startNum--)
            {
                //sprintf(numToWord, "%d", startNum);
                printf("%d", startNum);
                //fflush(stdout);
                sleep(1);
            }
        }
        tcsetattr(1,TCSAFLUSH,&oldTerm);
        return 0;
    Its possible my implementation is wrong. If so please do let me know how to fix it. The above code starts just above the for loop in my original post code.


    ***EDIT***
    Managed to solve the program
    Last edited by KingKush; 07-23-2018 at 04:57 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > ***EDIT***
    > Managed to solve the program
    Well tell us then, so anyone with a similar problem might benefit.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    16
    I wasnt exactly sure what fixed the problem other than adding a \n to the output

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. want to add countdown timer to my program
    By megazero1316 in forum C Programming
    Replies: 1
    Last Post: 12-14-2013, 01:00 PM
  2. Replies: 7
    Last Post: 08-28-2011, 09:01 PM
  3. Displaying output in new terminal window
    By RollingSkull in forum C Programming
    Replies: 12
    Last Post: 12-07-2006, 10:34 PM
  4. Countdown in Program
    By peckitt99 in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2006, 07:39 AM
  5. Whats wrong with my countdown program?
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2002, 01:15 PM

Tags for this Thread