Re: [問題] 海龍公式

看板C_and_CPP (C/C++)作者 (朱子)時間16年前 (2009/09/18 21:28), 編輯推噓3(305)
留言8則, 3人參與, 最新討論串2/2 (看更多)
寫個加法函式的例子給你看好了 #include<iostream> using namespace std; int add(int a, int b) { int result; result=a+b; return result; } int main() { int x,y,answer; cout<<"x="; cin>>x; cout<<"y="; cin>>y; answer=add(x,y); cout<<"x+y="<<answer<<"\n"; return 0; } 這樣應該就知道要怎麼改了吧? ※ 引述《diana76 (白鳳丸)》之銘言: : 剛學C++ : 學長出了一個作業_海龍公式 : 本人寫完後發現有點問題 : 回傳值的問題 : 但是不知道要怎麼解決通常不是可以在 : int main()的()中寫上函數嗎? : 有點不太懂這部份 : 不知道這種問題在這邊可不可以問? : 如果不行問的話會自D : #include<iostream> : #include<cmath> : using namespace std; : double a,b,c,s,area; : int math() : { : cout<<"Please input the three values for the lengths of the sides of a : triangle."<<endl<<"a="; : cin>>a; : cout<<"b="; : cin>>b; : cout<<"c="; : cin>>c; : s=(a+b+c)/2; : area=sqrt(s*(s-a)*(s-b)*(s-c)); : } : int main(int math) : { : if(a+b>c&&a+c>b&&b+c>a) : cout<<"The lengths cannot be those of the sides of a triangle."<<endl; : else : cout<<"The areaof the triangle is "<<area<<endl; : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.213.158

09/20 12:08, , 1F
他那個int main(int math)是正確的嗎 = =?
09/20 12:08, 1F

09/20 12:10, , 2F
自訂函數寫在寫在main()之前跟之後有甚麼差別呢??想了好久
09/20 12:10, 2F

09/20 12:14, , 3F
我看有的書會先在main()之前先宣告一次,然後main()寫完在寫
09/20 12:14, 3F

09/20 12:14, , 4F
出完整的自訂函數耶...感覺好麻煩= =;
09/20 12:14, 4F

09/20 12:17, , 5F
給大大推一下 XD
09/20 12:17, 5F

09/20 16:53, , 6F
寫在之後 那前面要先宣告函式的原型(prototype)
09/20 16:53, 6F

09/20 17:30, , 7F
回一樓,這樣不正確。main函式可以有引數但不是這樣用的
09/20 17:30, 7F

09/20 18:09, , 8F
謝謝兩位^^
09/20 18:09, 8F
文章代碼(AID): #1AiuiC2W (C_and_CPP)
討論串 (同標題文章)
本文引述了以下文章的的內容:
6
15
完整討論串 (本文為第 2 之 2 篇):
6
15
文章代碼(AID): #1AiuiC2W (C_and_CPP)