作者ric2k1 (Ric)
看板EE_DSnP
標題Re: [問題] 有關於int main()的問題
時間Sun Oct 7 21:28:48 2007
※ 引述《Davefox (Fox)》之銘言:
: 在作業題的第三題
: 我們宣告的不是都用double嗎
: 那為什麼主要執行的還是要打 int main()呢?
: 我有試過double main() 不對> <
: 這樣算double然後用int main不是很奇怪嗎?
double a = 22.0/(3.14159);
double b = 22.0*2.87757/(3.14159)/2.87757;
是宣告了兩個型態為 "double" 的 variables.
比方說, p42 in lecture note #1, 它宣告了三個型態為 "int" 的變數 "number1",
"number2", and "sum".
而 "int main()" 是宣告 main() 這個 function 的 return type 是 integer.
function 裏面變數的形態與 function 的 return type 沒有關係.
For example, you can have:
int main()
{
double a = 0.3;
double b = a * a;
cout << b << endl;
return 0;// 因為 main 要 return int,
// 而 0 is an int which conventionally means "execute successfully"
}
Note, in ISO C++ standard, the return type of "main()" must be "int".
And of course, for other functions, the return type can be anything,
including void.
For example,
double square(double a)
{
return (a * a);
}
For example,
void printNumber(double a)
{
cout << right << setw(10) << setprecision(2) << fixed << a << endl;
}
關於這些, 我下節課會補充一下.
: 還有為什麼code最後都要加system("pause");執行的時候才不會跳出呢?
: 可是課本上的code都沒有加這一行,這是用的編譯程式不同的問題嗎?
: 謝謝
我猜你是用 devC++?
因為 devC++ compile 完後執行的時候會開啟一個 dos window.
When the program terminates, the dos window will close.
And you will not be able to see the output results.
That's why we add a line system("pause") at the end.
Of course, it's not an intended part of the program so the code on the text
book will not include it.
Can anyone figure out how to set up the devC++ so that it will not close the
dos window at the end of program execution? Or can it have a fixed execution
window?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.131.190
1F:推 ilway25:there seems to be no solution on the internet.. 10/07 22:51
2F:推 flarehunter:用devC++ compile之後開cmd開啟執行檔可以嗎XD 10/07 23:15
3F:推 ilway25:這樣就沒問題阿 只是麻煩.. 10/07 23:46
4F:推 eemingh:哈 什麼是cmd阿??? 10/09 08:47