這裏第一次海報用戶definded函數的內部的比較。 請記住,我仍然對C++和編碼一般都很陌生。 我試圖重寫程序,詢問他們想要多少個號碼進行比對,然後他們進入每個數字的值用戶。我已經通過if和if語句實現了這一點,但現在我要求用用戶定義的函數來完成此操作。我到目前爲止的問題是,我輸入的第一個數字是保存的數字,但它會提示輸入更多數字,並且函數內部的for循環可以正常工作。一些建議,我不正確的做法將不勝感激。我已經聲明瞭所有需要的變量,所以我只把原型放在節省空間上。號碼的用戶指定量在C++
int main()
{
int DisplayGreatest(int,int);
do {
cout << "Please select an option" << endl;// Menu Options
cout << "A: Highest" << endl;
cout << "B:Lowest" << endl;
cout << "C: Quit " << endl;
cin >> Selection; // Menu Selection
cout << endl;
if ((Selection == 'a') || (Selection == 'A'))
{
cout << "How many numbers do you want to use?" << endl;
cin >> Aselection;
cout << "enter your first choice" <<endl;
cin >> currentAinput;
DisplayGreatest (Aselection,currentAinput);
cout << "The largest number is "<< currentAinput << endl;
}
}
}
功能:
int DisplayGreatest(int Aselection, int currentAinput)
{
int i;
int keepAinput=0;
for(i=1;i<Aselection;i++)
{
cin >> currentAinput;
if (currentAinput > keepAinput)
{
keepAinput=currentAinput;
}
}
return currentAinput;
}
非常感謝你有,我有原代碼while語句,不想貼B和C選項,因爲他們不該」!不會影響它。我正在使用的這本書建議將功能放在main之後,而原型放置在main之前。這有什麼真正的意義?我看到現在的原型放錯了位置,謝謝指出。 – geno84
不是真的,更多的是風格和品味。有些人喜歡首先擁有主人,並且您還可以在文件的開頭獲得所有功能的清單。但另一方面,你必須改變兩個地方。該程序的功能將是相同的。對於以後的項目,您很有可能會使用類和/或頭文件。 –