简体   繁体   中英

C++ need help fixing to get desired output

I am working on a programming challenge from "Starting Out With C++: Early Objects, 10th Edition". I have the code about halfway done, but the output isn't coming out how it is supposed to be, and I am wondering how to fix it. I have it attached here and the desired output.

#include <iostream>
using namespace std;

int main()
{
    float starting_num_of_organisms,
        average_daily_population_increase,
        size_of_daily_population;

    int num_of_days_to_multiply;

    cout << "Enter Starting Population: ";

    while (!(cin >> starting_num_of_organisms) || starting_num_of_organisms < 2) {
        cout << "Invalid. Population must be 2 or greater.";
        cout << "Enter starting population: ";
        cin.clear();
        cin.ignore(123, '\n');
    }

    cout << "Enter positive daily growth % (.1 must be entered as 10): ";

    while (!(cin >> average_daily_population_increase) || average_daily_population_increase < 0) {
        cout << "Invalid. Daily Population Growth \n"
             << " must be greater than 0. \n"
             << "Enter Daily Population Growth (%): ";
        cin.clear();
        cin.ignore(123, '\n');
    }

    average_daily_population_increase *= .01;

    cout << "Enter number of days to calculate: ";

    while (!(cin >> num_of_days_to_multiply) || num_of_days_to_multiply < 1) {
        cout << "Invalid. Number of days must not be less\n"
             << "than 1. Enter number of days to calculate: ";
        cin.clear();
        cin.ignore(123, '\n');
    }

    for (int i = 0; i < num_of_days_to_multiply; i++) {
        cout << "Population size for day " << (i + 1);
        cout << ": " << starting_num_of_organisms
             << endl;

        starting_num_of_organisms += (starting_num_of_organisms * average_daily_population_increase);
    }

    return 0;
}

Here is the current output:

Enter Starting Population: 896.896
Enter positive daily growth % (.1 must be entered as 10): 2.785
Enter number of days to calculate: 8
Population size for day 1: 896.896
Population size for day 2: 921.875
Population size for day 3: 947.549
Population size for day 4: 973.938
Population size for day 5: 1001.06
Population size for day 6: 1028.94
Population size for day 7: 1057.6
Population size for day 8: 1087.05

The desired output is something like this below. The numbers are just for reference, and do not have to be specific to those numbers.

Enter starting population (greater than 2): 896.896
Enter positive daily growth % (.1 must be entered as 10): 2.785
Enter number of days to calculate (greater than 1): 8
----------------------------------

Start Population: 896.90
Daily Percent Growth: 2.79%
Number of Days: 8

Day           Start   End
              Popl.   Popl.
----------------------------
1           896.90    921.87
2           921.87    947.55
3           947.55    973.94
4           973.94   1001.06
5          1001.06   1028.94
6          1028.94   1057.60
7          1057.60   1087.05
8          1087.05   1117.33

It appears as though your code does the correct calculations. Just format the output and it will look like the desired output.

printf("%-10s%10s%10s\n", "day", "start", "end");
for(int i = 0; i < 30; i++)
    printf("=");
    
printf("\n");
for (int i = 0; i < num_of_days_to_multiply; i++)
{
    printf("%-10u%10.2lf", i + 1, starting_num_of_organisms);

    starting_num_of_organisms +=
        (starting_num_of_organisms *
            average_daily_population_increase);
    printf("%10.2lf\n", starting_num_of_organisms);
}

Note I used printf from the C libs (<stdio.h>), but the same can be achieved with cout, just much more verbose. Output:

Enter Starting Population: 896.896                                                                                                              
Enter positive daily growth % (.1 must be entered as 10): 2.785                                                                                 
Enter number of days to calculate: 8                                                                                                            
day            start       end                                                                                                                  
==============================                                                                                                                  
1             896.90    921.87                                                                                                                  
2             921.87    947.55                                                                                                                  
3             947.55    973.94                                                                                                                  
4             973.94   1001.06                                                                                                                  
5            1001.06   1028.94                                                                                                                  
6            1028.94   1057.60                                                                                                                  
7            1057.60   1087.05                                                                                                                  
8            1087.05   1117.33

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM