作者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