2013-02-27 69 views
1
#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; 
} 

我使用的代碼塊,但我不知道如何清除屏幕。我搜索後發現在頭文件#include<cstdlib>內的system("cls");,但它顯示錯誤cstdlib: no such file of directory。我該怎麼辦 ?如何從簡單的C程序中清除屏幕?

+0

如果有疑問,請確實訪問這些鏈接,要知道哪個函數屬於哪個頭文件或哪個頭文件有哪個函數:-) [Alphabetical Index](http://pubs.opengroup.org/onlinepubs/ 009695399/idx/index.html)和[Headers Index](http://pubs.opengroup.org/onlinepubs/009695399/idx/headers.html) – 2013-02-27 02:59:10

+0

'main()' - >'int main(void)'和'rec();' - >'int rec(int x);' – 2017-03-05 05:31:30

回答

6

清除屏幕是正常的C程序的範圍之外。這取決於操作系統。

對於windows,你應該看看

對於unix,請查看

system()總是啓動一個子shell,它可能會或可能不會影響父程序的環境。你需要一個系統調用,但不是 a system()調用。


我並不總是知道這一點。我曾經(很久以前)在comp.lang.c中建議有人應該嘗試system("exit");關閉DOS程序周圍的窗口。但是,那當然是行不通的。我很快被建議在發佈前測試我的代碼。 :)

7

變化

#include <cstdlib> 

#include <stdlib.h> 

cstdlibC++頭文件,因此將用C不可用。

+0

只是另一個屏幕的閃光來臨。但curren屏幕仍然保持原樣,沒有清除屏幕 – 2013-02-27 02:42:26

+0

@RahulSubedi這真的是一個不相關的問題..看起來'system'打開一個新的終端,而不是使用當前的終端。你有沒有嘗試從命令行而不是從eclipse運行程序? – 2013-02-27 02:47:45

+0

我使用的是代碼塊 – 2013-02-28 06:07:04

4

你有很多的問題,在你的代碼....

但對於具體問題,請嘗試#include <stdlib.h>

+0

#include 也沒有工作。只是另一個窗口閃現n消失n當前窗口保持不變而沒有清除屏幕 – 2013-02-27 02:28:53

0

使用#include<stdlib.h>這就是定義清屏功能的地方。

-1

要使用system("cls")您需要標題<iostream>。這將允許所有system()類型執行。不確定它是否是C++頭文件,但它適用於我使用的編譯器。