How to clear screen from simple C program?

2019-04-15 21:02发布

#include <stdio.h>
#include <cstdlib>

rec();

main() 
{
    int a, fact;
    char q, n, y;
    printf("\nEnter any number ");
    scanf("%d", & a);
    fact = rec(a);
    printf("Factorial value = %d\n", fact);
    printf("do you want to exit.....(y/n):");
    scanf("%s" ,&q);
    if (q == 'n')
    {
        system("cls");
        main();
    }
    else
        return 0;
}

rec(int x) 
{
    int f;
    if (x == 1) 
        return 1;
    else 
        f = x * rec(x - 1);

    return f;
}

I'm using code blocks but I don't know how to clear the screen. I searched then found system("cls"); within header file #include<cstdlib>, but it shows the error cstdlib: no such file of directory. What should I do ?

5条回答
虎瘦雄心在
2楼-- · 2019-04-15 21:24

use the #include<stdlib.h> that's where the clear screen function is defined.

查看更多
做个烂人
3楼-- · 2019-04-15 21:26

Clearing the screen is outside the purview of a normal C program. It depends on the operating system.

For windows, you should look into .

For unix, look into or .

system() always launches a sub-shell which may or may not have any effect on the environment of the parent program. You do need a system-call, but not a system() call.


I didn't always know this. I once (long ago) suggested in comp.lang.c that someone should try system("exit"); to close the window around the DOS program. But that, of course, cannot work. And I was quickly advised to test my code before posting. :)

查看更多
再贱就再见
4楼-- · 2019-04-15 21:29

To use system("cls") you need the header <iostream>. This will allow all system() types to execute. Unsure if it is a C++ header file, but it works for the compiler that I use.

查看更多
混吃等死
5楼-- · 2019-04-15 21:41

Change

#include <cstdlib>

to

#include <stdlib.h>

cstdlib is a C++ header file, and thus will be unusable in C.

查看更多
放荡不羁爱自由
6楼-- · 2019-04-15 21:44

you have lots of problems in your code....

but for the specific problem, try #include <stdlib.h>

查看更多
登录 后发表回答