0

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
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    Why does every other C++ question use [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – SuperStormer Mar 04 '21 at 01:13
  • Where is the output of your original program? Sorry if I missed it... I just see the desired output. – Telescope Mar 04 '21 at 01:13
  • i will edit it and show the current output! – Jake Schultz Mar 04 '21 at 01:17
  • @Telescope do you know any other options i can use for it instead then? – Jake Schultz Mar 04 '21 at 01:20
  • 3
    @SuperStormer Because damn near every tutorial website, college source reference text, or illicit Herb Schildt-esque book on C++ does the same. I.e., *they're taught that*. (which I find ironic; they cant' teach how to user standard containers, iterators, or algorithms, but they teach them junk like `using namespace std;`). – WhozCraig Mar 04 '21 at 01:21
  • On a side note: `cin.ignore(123, '\n');` should be `cin.ignore(numeric_limits::max(), '\n');` – Remy Lebeau Mar 04 '21 at 01:22
  • 1
    @JakeSchultz what is the *actual problem* you are trying to solve? The numbers in your output are very close to the expected numbers, so what is the actual issue? Are you aware that floating-point math is not precise? Or is the issue just in *formatting* the numbers the same way as the expected output, ie in columns? Please clarify the actual issue. – Remy Lebeau Mar 04 '21 at 01:26
  • @RemyLebeau the issue is i am trying to get the rounding issue fixed as well as the formatting of the information to look like the desired output in that sort of table look. whereas mine is just the start popl. and i need the end popl. as well – Jake Schultz Mar 04 '21 at 01:29

1 Answers1

1

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
Lucas Streanga
  • 469
  • 2
  • 7
  • Look at [`std::setw()`](https://en.cppreference.com/w/cpp/io/manip/setw) and [`std::setprecision()`](https://en.cppreference.com/w/cpp/io/manip/setprecision) when using `std::cout` instead of `printf()` (which BTW, C++ code should be using `` and `std::printf()` instead of `` and `::printf()`). – Remy Lebeau Mar 04 '21 at 01:33
  • @RemyLebeau Yes, those will do the trick with cout. Make sure to include so the compiler doesn't yell at you. For heavily formatted output I find printf is easier to read and easier to write. Much more compact as well. – Lucas Streanga Mar 04 '21 at 01:34
  • C++20 added [`std::format()`](https://en.cppreference.com/w/cpp/utility/format/format), which is similar to `sprintf()`, but much safer. – Remy Lebeau Mar 04 '21 at 01:37
  • @RemyLebeau I was not aware of C++20's format(). Frankly, C++20 is not default used on most compilers, including mine, GCC. As for stdio vs cstdio - it makes very little difference. Just namespace semantics, basically if you don't name a function of your own printf it will make no difference at all. – Lucas Streanga Mar 04 '21 at 01:40
  • Thank you all so much! C++ is tricky so far but it's something I get excited about when learning so i appreciate all of your help! – Jake Schultz Mar 04 '21 at 01:41
  • @JakeSchultz Yeah C++ is by far my favorite language! Really nice features and you can really squeeze efficiency out of it that you just can't get out of other languages. – Lucas Streanga Mar 04 '21 at 01:42