CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: cout position

  1. #1
    Join Date
    Sep 2009
    Posts
    28

    cout position

    Hi all,

    Can anyone help with me positioning the output of std::cout in a console application? Basically I want a loop which outputs a sequence of characters (a single letter for example) in the same position each time, replacing the previous character.

    Thanks,

    Payne747

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: cout position

    That would have to be done with OS-specific methods. There's nothing about it in the standard library.

  3. #3
    Join Date
    Sep 2009
    Posts
    28

    Re: cout position

    Thanks, I figured standard libraries wouldn't allow such things on a stream, however I managed to cheat by simply using the carriage return \r along with std::flush to 'cheat' :P

    Code:
    char progBar[] = {'|', '/', '-', '\\'};
    int progPos = 0;
    
    while (something) {
        std::cout << "\r" << progBar[progPos] << std::flush;
        progPos++;
        if (progPos == 4) {
            progPos = 0;
       }
    }
    Ugly, but it seems to work, though it doesn't produce very consistent results depending on the loop conditions (mine happens to be waiting on InternetReadFile() so it's quite delayed at times).

  4. #4
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: cout position

    If you're programming in Windows you can use SetConsoleCursorPosition to position the cursor before a cout.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured