C++ jak wyczyscić ekran

0

Witam. Mam taki program;

#include <iostream>
int main()
{
    int a;
    std::cout <<"Wpisz liczbe naturalna" <<std::endl;
    std::cin>>a;
    std::cout <<"Wpisales liczbe " <<a;
    return 0;
}
 

i chcę, żeby po wpisaniu liczby naturalnej(czyli a) ekran się wyczyścił i żeby się wyświetliło tylko "Wpisałeś liczbę ..." Co mam tu poprawić?

3

Jeżeli korzystasz z Windowsa i nie zależy Ci na przenośności, użyj system("cls"), lecz jest to złe wyjście.
Jeżeli chcesz coś przenośnego, są dwa rozwiązania:
1.Skorzystaj np.z ncurses.
2.Wyświetl kilkanaście znaków nowej linii (to takie drobne "oszustwo" ;P)

6

Phi!
Może tego sporo, ale powinno działać na systemach:
-Windows
-Linux
-Unix
-Android
-Mac
-iOS

#ifdef __ANDROID_API__
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include <stdio.h>
#elif !defined(__APPLE__) || (TARGET_OS_IPHONE) || (TARGET_IPHONE_SIMULATOR)
    #include <stdlib.h>
#elif __APPLE__
    #include <unistd.h>
    #include <term.h>
#endif
 
void ClearScreen(){
#ifdef __ANROID_API__
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    int i = -1;
    for(;i<w.ws_row;++i) puts("");
#elif defined WIN32
    system("cls");
#elif defined __APPLE__
    #ifdef (TARGET_OS_IPHONE) || (TARGET_IPHONE_SIMULATOR)
        int i = -1;
        for(;i<70; ++i) puts("");
    #else
        if(!cur_term){
            int result;
            setupterm( NULL, STDOUT_FILENO, &result );
            if(result <= 0) return;
        }
        putp(tigetstr("clear"));
    #endif
#else
    system("clear");
#endif
}

/* ... */

ClearScreen();

( bez ios: https://ideone.com/lEe74R )
Dla sprostowania:
w androidowym oraz ios'owym terminalu (raczej) nie będzie paska przewijania, więc oszusto polegające na skoczeniu w dół nie powinno zostać zauważone.

0

@up a gdzie mam wrzucić kod programu?
@2xUP ale kiedy wpisuje system("cls") to wywala error: 'system' was not declared in this scope

1

Dodaj #include <cstdlib>

1
#ifdef __ANDROID_API__
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include <stdio.h>
#elif !defined(__APPLE__) || (TARGET_OS_IPHONE) || (TARGET_IPHONE_SIMULATOR)
    #include <stdlib.h>
#elif __APPLE__
    #include <unistd.h>
    #include <term.h>
#endif
 
void ClearScreen(){
#ifdef __ANROID_API__
    struct winsize w;
    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    int i = -1;
    for(;i<w.ws_row;++i) puts("");
#elif defined WIN32
    system("cls");
#elif defined __APPLE__
    #ifdef (TARGET_OS_IPHONE) || (TARGET_IPHONE_SIMULATOR)
        int i = -1;
        for(;i<70; ++i) puts("");
    #else
        if(!cur_term){
            int result;
            setupterm( NULL, STDOUT_FILENO, &result );
            if(result <= 0) return;
        }
        putp(tigetstr("clear"));
    #endif
#else
    system("clear");
#endif
}


#include <iostream>
int main()
{
    int a;
    std::cout <<"Wpisz liczbe naturalna" <<std::endl;
    std::cin>>a;
    
    ClearScreen(); //<--czyscimy ekran.
    
    std::cout <<"Wpisales liczbe " <<a;
    return 0;
}
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1