C Programming 的 exe 檔建立問題 ...

2009-04-04 10:38 pm
本人用的是 Visual C++ 2008 Express Edition

但發現用 C 寫出來的 exe 檔運行完畢後會自動關閉 ...

無法顯示結果 ...


以下面這個為例 , 這是一個猜數字遊戲的程式 :


------------------------------


int i, x, y;

int no = 0;

printf("Input the seed number : ");

scanf("%d", &no);

printf("\n");

srand(no);

y = rand() % 10 + 1;

printf("Range of x : 1 - 10\n");

printf("You'll have 5 chances to guess the number\n\n");

for (i=1; i<=5; i++)

{

printf("Game %d : x = ", i);

scanf("%d", &x);

if (x>10)

printf("\nError - Range of x : 1 - 10\n\n");

if (x<1)

printf("\nError - Range of x : 1 - 10\n\n");

if (x==y)

{

printf("\nCongratulations!\n");

printf("Game : %d\n", i);

break;

}

}


printf("Answer : %d\n\n", y);


-----------------------------------


debug 時確認無誤 ... 程式本身是沒有問題的 ...

但出來的 exe 檔只要猜對了數字 , 便會自動關上 ...

printf("\nCongratulations!\n");

printf("Game : %d\n", i);

printf("Answer : %d\n\n", y);

以上三項結果沒顯示出來 exe 檔便自動關上了 ...

想問問該怎樣才能讓它不自動關上 , 以顯示結果 ?


--------------------------

另外 ,

本人的 exe 檔是在放置該程式的資料夾裡的 Debug 資料夾找出來的 ,

請問是否正確 ? 還是有其他方法去建立 exe 檔 ?

回答 (1)

2009-04-05 2:29 am
✔ 最佳答案
It is a problem related to console programs which closes the window and program exits.
If you add a statement at the end which requires input, then the console window will remain awaiting user input.
For example,
system("pause");
will print "press a key to continue..." and awaits input.
You can also add personalized messages before closure, such as:
printf("Thank you for executing the >>> program, press a key to continue...\n");
getchar();
PM me if you require more details.


收錄日期: 2021-04-13 16:33:04
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090404000051KK00818

檢視 Wayback Machine 備份