CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2013
    Posts
    39

    Change Output of Console

    is there a way to change output of console without clearing the screen? so making some kind of animation?

    e.g.

    I have used

    Code:
    cout << "Hello";
    is there any way I can erase last "lo" and replace it by "p" so I will have "Help" without clearing the screen ? just changing the output like some text file?

    I tried using something like this

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "abcd";
    	long pos = cout.tellp();
    	cout.seekp(pos-2);
    	cout.write("ef", 2);
    	cout.flush();
    	cin.get();
    	return 0;
    }
    but it doesnt work, there is still a "abcd"...

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Change Output of Console

    Use the special console API functions.
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    You can also try using backspace. The following prints "help"

    Code:
    int main()
    {
    	cout << "hello" << "\b\b" << "p " << endl;
    	return 0;
    }
    The backslash character constants are
    \b backspace
    \f formfeed
    \n newline
    \r return
    \t horizontal tab
    \" double quote
    \' single quote
    \\ backslash
    \v vertcal tab
    \a alert
    \N octal constant
    \xN hexadecimal constant
    \? question mark
    Last edited by 2kaud; November 4th, 2013 at 01:00 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Oct 2013
    Posts
    39

    Re: Change Output of Console

    this will print the help, but it wont print hello and then erase "lo" and replace it by "p", well yeah it does that ,but before it shows up on the screen

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Change Output of Console

    Code:
    int main()
    {
    	cout << "hello";
    	Sleep(2000);
    	cout << "\b\b" << "p ";
    	return 0;
    ]
    If you need anything more sophisticated you should be looking at the special console functions. I gave the link in post #2.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Oct 2013
    Posts
    39

    Re: Change Output of Console

    Quote Originally Posted by 2kaud View Post
    Code:
    int main()
    {
    	cout << "hello";
    	Sleep(2000);
    	cout << "\b\b" << "p ";
    	return 0;
    ]
    If you need anything more sophisticated you should be looking at the special console functions. I gave the link in post #2.
    ty, this one works, I guess mine didnt work since I was using cin.get() to go to the next step and it couldn't delete previous chars since there werent any on that line... :/

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