Stuck with '\b'

This is a discussion on Stuck with '\b' within the C++ Programming forums, part of the General Programming Boards category; I want to print 0 to 9 in an interval of 1 sec between each number AND the previous number ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    Stuck with '\b'

    I want to print 0 to 9 in an interval of 1 sec between each number AND the previous number must be overwritten.

    i.e. The numbers should be printed at the same location on the screen. But this is not working. Here is my code:

    Code:
    #include <iostream>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
      int i = 0;
      for(i=0; i<10; i++)
        {
          cout << i;
          sleep(1);
          cout << '\b';
        }
    
      return 0;
    }

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What is the output?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    The program is waiting for 10 secs and exits without any output.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    try this:

    cout << i << flush;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    28

    Thumbs up

    Thanks magos... It works.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    this also works:
    Code:
    #include <iostream>
    #include <unistd.h>
    
    using namespace std;
    
    int main()
    {
      int i = 0;
      for(i=0; i<10; i++)
        {
          cout <<"\b \b"; //backspace, space, backspace
          cout << i;
          sleep(1);
        }
    
      return 0;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  2. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  3. Program stuck in infinite loop-->PLEASE HELP
    By Jedijacob in forum C Programming
    Replies: 5
    Last Post: 03-26-2005, 11:40 AM
  4. Stuck on random generating
    By Vegtro in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2003, 07:37 PM
  5. stuck ky
    By JaWiB in forum Tech Board
    Replies: 2
    Last Post: 06-15-2003, 08:28 PM

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