How to clear C++ console?

Project is Calculator. If user enters the input 1 + 1 = 2 and if user presses any key again, then console clears/refreshes and starts again like, enter the first value, second value, etc.. and repeats. How to do that? I don't want this command as system("CLS);, nty. It's evil :D
Declare const std::string clear( 100, '\n' ) ;

And then, to clear the screen: std::cout << clear ;
JLBorges, it works but I don't want it to clear the entire CONSOLE. I wanted it to refresh back to the point where user has to input a number to add/subtract, etc..
> I wanted it to refresh back to the point where user has to input a number to add/subtract, etc..

What does your program look like now?
#include <iostream>
#include <windows.h>
using namespace std;

int choice;
float num1, num2, result;

template <class T>
void get_number(T& choice)
{
while(!(cin >> choice))
{
cin.ignore(1000, '\n');
}
}

int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 3); cout << "CALCULATOR" << endl;
SetConsoleTextAttribute(h, 3); cout << "Made by Kristen\n\n" << endl;
SetConsoleTextAttribute(h, 7);


while(true)
{

cout << "\n1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit\n" << endl;
cout << "What would you like to do? ";

get_number(choice);


if(choice == 5)
break;
if(choice < 1 || choice > 4)
continue;

cout << "\nPlease enter the first value: ";
get_number(num1);
cout << "Please enter the second value: ";
get_number(num2);

switch(choice)
{
case 1:
if(choice == 1)
result = num1 + num2;

case 2:
if(choice == 2)
result = num1 - num2;

case 3:
if(choice == 3)
result = num1 * num2;

case 4:
if(choice == 4)
result = num1 / num2;
}
cout << "\nThe result is: " << result << "\n";


SetConsoleTextAttribute(h, 2); cout << "\n\nThanks for using me!\n\n";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>

template <class T>
void get_number( T& number )
{
    while( !( std::cin >> number ) )
    {
        std::cin.ignore( 1000, '\n' );
    }
}

int main()
{
    const std::string header = "CALCULATOR\n"
                               "Made by Kristen\n\n\n"
                               "1. Add\n"
                               "2. Subtract\n"
                               "3. Multiply\n"
                               "4. Divide\n"
                               "5. Exit\n\n"
                               "What would you like to do? ";

    const std::string clear = std::string( 100, '\n' ) + header ;

    int choice = 0 ;
    while( choice != 5 )
    {
        std::cout << clear ;
        get_number(choice);


        if(choice == 5)
            break;
        if(choice < 1 || choice > 4)
            continue;

        std::cout << "\nPlease enter the first value: ";
        double num1 ;
        get_number(num1);

        std::cout << "Please enter the second value: ";
        double num2 ;
        get_number(num2);

        double result ;

        switch(choice)
        {
        case 1:
            if(choice == 1)
                result = num1 + num2;

        case 2:
            if(choice == 2)
                result = num1 - num2;

        case 3:
            if(choice == 3)
                result = num1 * num2;

        case 4:
            if(choice == 4)
                result = num1 / num2;
        }

        std::cout << "\nThe result is: " << result << "\n";

        std::cout << "enter any character to continue:" ;
        char ch ; std::cin >> ch ;
    }

    std::cout << "\n\nThanks for using me!\n\n";
}
That is a good code, I never thought that I can do string clear :o. Thanks man. I learned something new. I tweaked my code a little and seriously.. I went for system :D.

#include <iostream>
#include <windows.h>
#ifdef __cplusplus__
#include <cstdlib>
#else
#include <stdlib.h>
#endif __cplusplus__
using namespace std;

int choice;
float num1, num2, result;

template <class T>
void get_number(T& choice)
{

while(!(cin >> choice))
{
cin.ignore(1000, '\n');
}
}

int main()
{

HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h, 3); cout << "CALCULATOR" << endl;
SetConsoleTextAttribute(h, 3); cout << "Made by Kristen Ungur\n\n" << endl;
SetConsoleTextAttribute(h, 7);




while(true)
{

cout << "\n1. Add" << endl;
cout << "2. Subtract" << endl;
cout << "3. Multiply" << endl;
cout << "4. Divide" << endl;
cout << "5. Exit\n" << endl;
cout << "What would you like to do? ";

get_number(choice);


if(choice == 5)
break;

if(choice < 1 || choice > 4)
continue;

cout << "\nPlease enter the first value: ";
get_number(num1);
cout << "Please enter the second value: ";
get_number(num2);

switch(choice)
{
case 1:
if(choice == 1)
result = num1 + num2;

case 2:
if(choice == 2)
result = num1 - num2;

case 3:
if(choice == 3)
result = num1 * num2;

case 4:
if(choice == 4)
result = num1 / num2;
}
cout << "\nThe result is: " << result << "\n";

cout << "\nEnter any character to continue: ";
char ch; cin >> ch;

if (system("CLS")) system("clear");

}
SetConsoleTextAttribute(h, 2); cout << "\n\nThanks for using me!\n\n";
}


This works also :3.
1
2
3
4
5
6
7
8
9
10
11
12
13
/* --- Function: void my_clearscrn(void) --- */

void my_clearscrn(void)
{
#ifdef __unix__
  system("clear");

#elif _WIN32
  system("cls");

#endif
}
Last edited on
Topic archived. No new replies allowed.