Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6748793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:36:33+00:00 2026-05-26T12:36:33+00:00

I am trying to clear the console in C++. I know printing a number

  • 0

I am trying to clear the console in C++. I know printing a number of newlines is a bad practice, as it can be slow and is not always reliable to completely clear the console window, but I have researched multiple options and have found almost no other solutions besides system("cls"), which is an even worse option.

Essentially, I have used the line cout << string(100, '\n'); but I am getting a near-unidentifiable error when I try to run the program.

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)

I have also researched this, and found that most explanations were too complicated for me as a beginning C++ programmer to understand, or completely unrelated to my problem.

My questions are (1) is there a way to fix this error, and (2) could there be a better, cross-platform way of clearing the console other than printing 100 newlines?

I also heard of Console.clear(), but I’m unsure if this is cross-platform. From what I’ve seen, it looks more like a Windows command. I’ve also heard of the curses library, which I was willing to research and use, until I read somewhere that it was not recommended to use the functions which I am familiar with coupled with the curses library functions.

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:36:34+00:00Added an answer on May 26, 2026 at 12:36 pm

    About your error… you have to…

    #include <iostream> 
    #include <string>
    
    using namespace std;
    

    If you are using just windows use windows console API.
    If you are using a linux\unix terminal, use escape codes.
    You can do a #if to choose between the two methods.

    On linux\unix use the write function defined in in this way:

    write(1,"\E[H\E[2J",7); // we use ANSI escape sequences here.
    

    Here is the microsoft page that explain how to do that.

    http://support.microsoft.com/kb/99261

    The really bad console api microsoft use for the console always makes me angry 🙂 why 100 lines of code to clear a screen? 🙂

    Now the if… you should create a clearscreen.h file and a clearscreen.cpp file.

    In clearscreen.h we just put our function.

     void clearconsole();
    

    In clearscreen.cpp we put our code for both operative systems

    #ifdef _WIN32 || _WIN64
    
        #include <windows.h>
    
        void clearconsole()
        {
            ...
            // 100 lines of codes copied from microsoft article
        }
    
    #else
    
        #include <unistd.h>
    
        void clearconsole()
        {
            write(1,"\E[H\E[2J",7);
        }
    
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to clear images using external jquery file but it's not showing
I'm trying to migrate to JUnit 4 and I'm not clear about the correct
I am trying to clear the cache for my integration testing. I could find
I am trying to clear the value attribute of an 'Other' form input when
I am trying to clear up a few things in my head about implementing
I'm trying to come up with some clear and concise names for a Permission
I'm trying to get a clear picture of what's involved practically when doing Android/ADK
I am trying to create a clear method that would clear the array I
I am trying to make my code more clear using the include function of
I am trying to write a sample Raw socket program to clear my understanding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.