[C++]Quick question.

Super Mexican
Loyal Member
Joined
Jun 26, 2008
Messages
1,517
Reaction score
2
I'm making a txt based game in C++ (Console Application).
I want the screen to clear and start a new screen when you type in your answer to the question.
How do I do this?

-------------------------------------------------------------
It asks you if your ready to start.
Yes or no?

You type yes or no and then the screen clears and starts if you typed yes.
-------------------------------------------------------------

Any help?
 
Well.. I dont know if this helps you, but in C# it'd be:
(Did from my head so idk if it's 100% error-proof)

PHP:
using System;

class Test
{
     static void Main()
     {
            this.showStuff();
     }

     static void Test()
     {
            //Constructor for class TEST
     }

     public void Welcome()
     {
            Console.Write("Welcome to my game!");
     }

     public void showStuff()
     {
     string read = null;
      Console.Write("Are you ready to start? (yes/no)" + Environment.NewLine + Environment.NewLine);
      Console.Write("Answer: ");
      read = Console.ReadLine();

          if (read == "yes")
          {
           Console.Clear();
           this.Welcome();
          }
          else
          {
           Console.Clear();
           System.Threading.Thread.Sleep(1000); //Waits 1 sec
           Environment.Exit(0);
          }
     }
}
 
To think of it, surprises me that I never needed to play around with locations in console apps, but there are at least these solutions:

You could system("cls"), but after a little research it seems system calls are a bad practice (some say..)

Some suggest printing 25 (or however high your console is) end-lines, or then you could print 25*80 = 1000 spaces, as the default console screen, learned from DOS times, is probably this big.

I have the feeling neither of these is the optimal solution, but until someone steps in with it, try which one is fastest and let us know.

This topic brings another question to my mind, it's not possible to print, say just 1 character, to a specific screen location (console apps) without altering the rest of the screen, is it?

EliteGM: there's no Console-class with Clear() for C++, at least that I would've run into, so I doubt your solution helps here.
 
This topic brings another question to my mind, it's not possible to print, say just 1 character, to a specific screen location (console apps) without altering the rest of the screen, is it?

There is.

Code:
#include <iostream>
#include <windows.h>

HANDLE hOut;
COORD Position;

int main()
{
    SetConsoleTitle("Title");
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    system("cls");

    Position.Y += 4;
    SetConsoleCursorPosition(hOut, Position);
    std::cout << "This is the 4th line";    
}
 
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <windows.h>

void ClearScreen()
{
    HANDLE stdOutHndl = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD startCoord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO bufInfo;

    GetConsoleScreenBufferInfo(stdOutHndl, &bufInfo);
    FillConsoleOutputCharacter(stdOutHndl, '\0', bufInfo.dwSize.X * bufInfo.dwSize.Y, startCoord, &count);
    SetConsoleCursorPosition(stdOutHndl, startCoord);
}

int main()
{
    std::string ans;
    std::cout << "Ready to start?\n" << "Yes or no?\n";
    std::cin >> ans;

    std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);
    if (ans == "yes") {
        ClearScreen();    

    }
}

Here's an example of how you should do it (under windows). Using system("cls") is generally thought of as being bad due to the overhead involved with system() calls.
 
Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <windows.h>

void ClearScreen()
{
    HANDLE stdOutHndl = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD startCoord = {0, 0};
    DWORD count;
    CONSOLE_SCREEN_BUFFER_INFO bufInfo;

    GetConsoleScreenBufferInfo(stdOutHndl, &bufInfo);
    FillConsoleOutputCharacter(stdOutHndl, '\0', bufInfo.dwSize.X * bufInfo.dwSize.Y, startCoord, &count);
    SetConsoleCursorPosition(stdOutHndl, startCoord);
}

int main()
{
    std::string ans;
    std::cout << "Ready to start?\n" << "Yes or no?\n";
    std::cin >> ans;

    std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);
    if (ans == "yes") {
        ClearScreen();    

    }
}

Here's an example of how you should do it (under windows). Using system("cls") is generally thought of as being bad due to the overhead involved with system() calls.

Thank you sooo much Izu. It worked perfectly.
Thanks alot!
 
My bad, should've specified I was asking about a more general solution, one that would work regardless of OS. Thanks a bunch for this one, though!

Yeah. Only simple console applications are cross platform, which I prefer. For more advanced console feature's you need to use OS specific commands. In that case it's best to write your own console handler which is cross-OS. I currently am doing that :) (or at least, it is possible with the library I'm making).
 
Back