Reprinting a line

This is a discussion on Reprinting a line within the C++ Programming forums, part of the General Programming Boards category; Hello All... I am writting a program and I am using a count down in it and I was just ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    Reprinting a line

    Hello All...
    I am writting a program and I am using a count down in it and I was just wondering how I could rewrite a line instead of getting a new line every time a second passes.

    This is the output I get now:
    "Remaining Time: 10"
    "Remaining Time: 9"
    "Remaining Time: 8"
    and do on

    I would like:
    "Remaining Time: <Have just this number change instead of a whole new line>"

    Here is the count down code I am using:

    Code:
    #include <iostream.h>
    #include <unistd.h>
    
    int main()
    {
        int count = 10;
        do
        {
            cout<<"Remaining Time: "<<count<<endl;
            count = count - 1;
            sleep(1);
        }
        while(count > 0);
    }
    Really Appreciate The Help!
    Last edited by Jperensky; 04-24-2002 at 06:23 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    8
    You could try gotoxy(), more info in the FAQ.

  3. #3
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    Code:
    #include <iostream.h>
    #include <unistd.h>
    
    int main()
    {
        int count = 10;
        cout<<"Remaining Time: ";
        do
        {
            cout <<count;
            count = count - 1;
            sleep(1);
            system("CLS");
        }
        while(count > 0);
    }
    the best things in life are simple.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    59

    For Unix...

    The CLS is a good (using CLEAR for unix)... that works but does anyone know or a way to do that without having to clear the entire screen?
    Last edited by Jperensky; 04-24-2002 at 07:37 PM.

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Probably going against every standard known to man, but what about something along the following lines?

    Code:
    #include <stdio.h> 
    #include <conio.h> //included for clrscr, clear the screen however you like
    #include <dos.h> // included for sleep on my DOS compiler
    
    int main ()
    {
    clrscr();
    int count = 10;
            do
            {
            printf ("\r Remaining time: %i ",count);
            count = count -1;
            sleep(1);
            }
            while (count > 0);
    return 0;
    }
    No reason you couldn't use cout with "\r" I guess, for some reason my fingers wanted printf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-19-2009, 12:44 AM
  2. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 09:17 PM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM
  5. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21