2
#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 ?

slugster
  • 49,403
  • 14
  • 95
  • 145
Rahul Subedi
  • 131
  • 1
  • 3
  • 12
  • When in doubt, please do visit these links, to know, which function belongs to which header file or which header file has which function :-) [Alphabetical Index](http://pubs.opengroup.org/onlinepubs/009695399/idx/index.html) and [Headers Index](http://pubs.opengroup.org/onlinepubs/009695399/idx/headers.html) – nIcE cOw Feb 27 '13 at 02:59
  • `main()` -> `int main(void)` and `rec();` -> `int rec(int x);` – MD XF Mar 05 '17 at 05:31

5 Answers5

9

Change

#include <cstdlib>

to

#include <stdlib.h>

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

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • just a flash of another screen comes n went off. but the curren screen remains as it is without clearing screen – Rahul Subedi Feb 27 '13 at 02:42
  • @RahulSubedi That is an unrelated problem really.. Looks like `system` is opening a new terminal instead of using the current one. Have you tried running the program from the command line instead of from eclipse? – Karthik T Feb 27 '13 at 02:47
  • @RahulSubedi luser droog has some insights about your clearing the screen problem – Karthik T Feb 28 '13 at 06:15
  • I have tried this but I am getting the error: `sh: cls: command not found` – maxisme Oct 21 '14 at 21:39
  • @Maximilian try `clear` instead. That needs to be a func supported by your shell – Karthik T Oct 22 '14 at 04:16
7

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. :)

luser droog
  • 18,988
  • 3
  • 53
  • 105
4

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

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

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • #include also didnt worked. just a flash of another window come n goes away n the current window remains same without clearing screen – Rahul Subedi Feb 27 '13 at 02:28
-1

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

jackotonye
  • 3,537
  • 23
  • 31
-1

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.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Mike
  • 1