2014-02-07 21 views
0

這裏第一次海報用戶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;    
} 

回答

0

int DisplayGreatest(int,int)或許應該在全局命名空間中聲明,即:

int DisplayGreatest(int, int); 

int main() 
{ 
} 

int DisplayGreatest (int Aselection, int currentAinput) 
{ 
} 

此外,您還可以在輸入一個循環,但它是有點更自然地採取整個vector價值整數,然後找到最高/最低使用相同的想法(循環通過每一個,並保存當前最高/最低)。

編輯:哦,我知道現在你的問題是什麼。您需要在函數末尾返回keepAinput,並將其分配給currentAinput或其他變量並打印,無論您將其分配給哪個。或者只是直接打印結果,即:

cout << "Highest number is " << DisplayGreatest(Aselection, currentAinput) << endl; 
1

你在你的代碼的幾個錯誤:

  • 不使用返回值從函數DisplayGreatest
  • 你永遠比較第一你與別人(你連續兩次CIN沒有第一個比較之前)
  • 聲明中錯誤的範圍功能DisplayGreatest進入第一個值(應該是在全球範圍內,或將整個功能前主)
  • 你沒有結束條件在主你做循環。 (缺少while語句,也許你有你自己的版本)
  • 您在返回的DisplayGreatest功能,不是最大的一個最後輸入的值。

這裏是一個功能版本:

#include <iostream> 

using namespace std; 

int DisplayGreatest(int Aselection) 
{ 
    int keepAinput = 0; 
    int currentAinput; 

    do { 
     cin >> currentAinput; 
     if (currentAinput > keepAinput) 
     { 
      keepAinput = currentAinput; 
     } 
    } while(--Aselection > 0); 

    return keepAinput; 
} 

int main() 
{ 
    char Selection; 

    do { 

     int Aselection; 
     int currentAinput; 

     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; 
       cout << "The largest number is "<< DisplayGreatest (Aselection) << endl; 
     } 
    } while(Selection != 'C' && Selection != 'c'); 
} 
+0

非常感謝你有,我有原代碼while語句,不想貼B和C選項,因爲他們不該」!不會影響它。我正在使用的這本書建議將功能放在main之後,而原型放置在main之前。這有什麼真正的意義?我看到現在的原型放錯了位置,謝謝指出。 – geno84

+0

不是真的,更多的是風格和品味。有些人喜歡首先擁有主人,並且您還可以在文件的開頭獲得所有功能的清單。但另一方面,你必須改變兩個地方。該程序的功能將是相同的。對於以後的項目,您很有可能會使用類和/或頭文件。 –