2016-02-15 89 views
-3

我試圖從一個函數返回一個值,該函數有一個參數,我只是不知道如何去獲取這些值。我知道我可能做了一些錯誤的代碼,這就是爲什麼我在這裏。返回參數並在其他函數中使用它們

下面是代碼:

// ask user if male or female 
// complete calculations depending on result 
// ask user for: weight in lbs, height in inches and age. 

#include <iostream> 

// function for users height in inches 
double usersHeight(double height) 
{ 
    return height; 
} 

//function for users age 
int usersAge(int age) 
{ 
    return age; 
} 

// function for users weight in lbs 
double usersWeight(double weight) 
{ 
    return weight; 
} 

// mens bmr (basal metabolic rate) 
double calculateBmrForMen() 
{ 
    // return calculations 
    // Men: BMR = 66 + (6.23 x weight in pounds) + (12.7 x height in inches) - (6.8 x age in years) 
    double BMR = 66 + (6.23 * usersWeight(weight/*how do I get user input and here without causing any errors?*/)) 
        + (12.7 * usersHeight(height/* and here*/)) 
        - (6.8 * usersAge(age/*and here*/)); 
    return BMR; 
} 

// womens bmr 
double calculateBmrForWomen() 
{ 
    // return calculations 
    // Women: BMR = 655 + (4.35 x weight in pounds) + (4.7 x height in inches) - (4.7 x age in years) 

    double BMR = 655 + (4.35 * usersWeight()) + (4.7 * usersHeight()) - (4.7 * usersAge()); 
    return BMR; 
} 

int main() 
{ 
    /*char a; 

    std::cout << "Are you Male or Female: " << std::endl; 
    std::cout << "Press M if Male\nPress F if female" << std::endl; 
    std::cin >> a; 

    if (a == 'm') 
    { 
     calculateBmrForMen(); 
    } 
    else if (a == 'f') 
    { 
     calculateBmrForWomen(); 
    }*/ 

    std::cin.get(); 
    std::cin.clear(); 
} 
+1

'weight/*如何獲得用戶輸入,這裏沒有引起任何錯誤?* /)'使用一個名爲'weight'和'std :: cin'的局部變量可能是什麼? –

+0

我不知道你爲什麼在這裏得到-4,雖然這對人們宣佈他們的DV很好,但我們允許匿名投票,所以我們必須猜測。這可能是沒有跡象表明您已經完成了什麼調試,或者問題是什麼。如果將來你可以更具體,那通常會有幫助。此外,不要在帖子中添加投票評論是一個好主意,因爲這可以自相矛盾地鼓勵更多的DV! HTH。 – halfer

回答

0

看你如何使用這些功能,我猜測你想提示用戶輸入用戶的年齡在功能userAge並返回值調用函數。

如果這是你想做些什麼?

這是你的功能應該是什麼樣子。

int usersAge() 
{ 
    int age; 
    std::cout << "Please enter age: "; 
    std::cin >> age; 

    // If the user entered incorrect data, try again. 
    if (!std::cin) 
    { 
     std::cout << "Unable to read age. Try again.\n"; 
     std::cin.clear(); 
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 
     return userAge(); 
    } 

    return age; 
} 

確保添加

#include <limits> 

能夠使用std::numeric_limits

對其他功能做類似的更改。

+0

謝謝你的迴應。如果你不介意,你能解釋「(std :: numeric_limits :: max(),'\ n');」作品?我從來沒有見過,如果沒有,那很好。 親切的問候 – ShaneLee

+0

@ShaneLee。你可以在SO中找到很多使用'istream :: ignore'的答案。你可以在http://en.cppreference.com/w/cpp/io/basic_istream/ignore上看到更多的細節。 –

0

請修改您的代碼,如下所示; 希望這有助於。

double usersHeight() 
{ 
    double height; 
    cout << "Ënter Height..." << endl; 
    cin >> height; 
    return height; 
} 

//function for users age 
int usersAge() 
{ 
    int age; 
    cout << "Ënter age..." << endl; 
    cin >> age; 
    return age; 
} 

// function for users weight in lbs 
double usersWeight() 
{ 
    double weight; 
    cout << "Ënter weight..." << endl; 
    cin >> weight; 
    return weight; 
} 


double calculateBmrForMen() 
{ 
    // return calculations 
    // Men: BMR = 66 + (6.23 x weight in pounds) + (12.7 x height in inches) - (6.8 x age in years) 
    double BMR = 66 + (6.23 * usersWeight()) 
        + (12.7 * usersHeight()) 
        - (6.8 * usersAge()); 
    return BMR; 
} 

// womens bmr 
double calculateBmrForWomen() 
{ 
    // return calculations 
    // Women: BMR = 655 + (4.35 x weight in pounds) + (4.7 x height in inches) - (4.7 x age in years) 

    double BMR = 655 + (4.35 * usersWeight()) + (4.7 * usersHeight()) - (4.7 * usersAge()); 
    return BMR; 
} 

int main() 
{ 
    char a; 
    double BMI; 
    std::cout << "Are you Male or Female: " << std::endl; 
    std::cout << "Press M if Male\nPress F if female" << std::endl; 
    std::cin >> a; 
    if (a == 'm') 
    { 
     BMI = calculateBmrForMen(); 
    } 
    else if (a == 'f') 
    { 
     BMI = calculateBmrForWomen(); 
    } 

    cout << "The BMI is..."<< BMI << endl; 
    std::cin.get(); 
    std::cin.clear(); 
} 
相關問題