在C ++中进行分配时遇到麻烦

时间:2019-04-22 20:56:41

标签: c++

我一直在从事一项作业,我的代码正在运行,但结果差强人意。我知道问题与四舍五入有关,但我似乎无法弄清楚问题出在哪里。我已经包括了作业详细信息以及我得到的结果与预期结果。任何帮助表示赞赏。

图像链接https://imgur.com/a/bqIcxfT

'''

// This program will display the size of a population for given number of years
// taking into account annual birth and death rates as well as the number of 
// people who move away and move into the area.

#include <iostream>
#include <iomanip>

using namespace std;

//Function prototype 
double calculatePop (double, double, double, int, int);



int main ()
{
    double P;           // Starting population
    double  B;      // Annual birth rate 
    double  D;      // Annual death rate 
    int A;          // Average number of people who arrive
    int M;          // Average number of people who move away
    int nYears;     // The number of years to display

    cout << "This program calculates population change." << endl;

    // Set numeric formatting 
    cout << setprecision(0) << fixed;

    // Get starting population size
    cout << "Enter the starting population size: ";
    cin >> P;
    while (P<2){
        cout << "Starting population must be 2 or more.";
        cout << "Please re-enter:";
        cin >> P;}

    // Get the annual birth rate
    cout << "Enter the annual birth rate (as % of current population): ";
    cin >> B;
    while (B<0){
        cout << "Birth rate percent cannot be negative.";
        cout << "Please re-enter:";
        cin >> B;}

    B/=100;

    // Get annual death rate
    cout << "Enter the annual death rate (as % of current population): ";
    cin >> D;
    while (D<0){
        cout << "Death rate percent cannot be negative.";
        cout << "Please re-enter:";
        cin >> D;}

    D/=100;

    // Get number of people who arrive

    cout << "How many individuals move into the area each year? ";
    cin >> A;
    while (A<0){
        cout << "Arrivals cannot be negative.";
        cout << "Please re-enter:";
        cin >> A;}

    // Get number of people who move away
    cout << "How many individuals leave the area each year? ";
    cin >> M;
    while (M<0){
        cout << "Departures cannot be negative.";
        cout << "Please re-enter:";
        cin >> M;}

    // Get number of years to see data for
    cout << "For how many years do you wish to view population changes? " << endl << endl;
    cin >> nYears;
    while (nYears<1){
        cout << "Years must be one or more.";
        cout << "Please re-enter:";
        cin >> nYears;}

    cout << "Starting population: " << P << endl << endl;

    //Display the population to user
    for (int y=1; y<=nYears; y++)
    {
        P = calculatePop(P, B, D, A, M);
        cout << "Population at the end of year " << y << " is " << P << ".\n";  
    }
}

double calculatePop (double P, double B, double D, int A, int M)
{
    double N;       //New Population Size 
    N = P + (B*P) - (D*P) + A - M;
    return N;
}

'''

1 个答案:

答案 0 :(得分:1)

该值已正确计算,但输出方式与赋值方式不同。将setprecision(0)fixed一起设置会将数字四舍五入到最接近的整数,而赋值中显示的结果是截断的数字。要截断结果,请使用

cout << "Population at the end of year " << y << " is " << int(P) << ".\n";
相关问题