This is an assignment for my programming class, I'm looking for some constructive criticism.
The exact question is:
Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. A loop should display the size of the population for each day.
Input Validation: Do not accept a number less than 2 for the starting size of the population. Do not accept a negative number of average daily population increase. Do not accept a number less than 1 for the number o0f days they will multiply.
I'm not very happy with the way I asked for the input of the percentage of increase a day, and if the number is too high, the dos window won't display all of the results (Not sure if I can control this or not). I would really appreciate some suggestions for improvement on this program, as well as any errors that I have missed.
#include<iostream>
#include<iomanip>
usingnamespace std;
int main()
{
int days;//number of days the population will increase
float population_increase;//percentage of increase each day
float population_size;//starting size of population
cout << "5.11 Population\n\n\n";
cout << "What is the starting size of the population\n";
cout << "(Please specify a whole number)\n";
cin >> population_size;
//if the user enters a number less than 2, ask for a number larger than 1
while(population_size < 2)
{
cout << "\n\nPlease specify a number larger than 1.\n";
cin >> population_size;
}
cout << "\n\nWhat will the average daily population percentage increase be?\n";
cout << "(Remember, whole numbers like 1 or 2 are the same as 100% and 200%)\n";
cin >> population_increase;
//if the user enters a negative percentage, ask for a postive percentage
while(population_increase < 0.0)
{
cout << "\n\nPlease specify a non-negative percentage.\n";
cin >> population_increase;
}
cout << "\n\nFor how many days will this increase take place?\n";
cout << "(Please specify a whole number)\n";
cin >> days;
//if user enters a number less than 1, ask for a number larger than 0
while(days < 1)
{
cout << "\n\nPlease specify a number larger than zero.\n";
cin >> days;
}
cout << "\n\n";
cout << setprecision(0) << fixed;
for (int count = 1; count <= days; count++)
{
cout << "------------------------------------------------------\n";
cout << "Day " << count << endl;
population_size = population_size+(population_increase * population_size);//the final result after the growth
cout << "Population Size:" << population_size << endl;
}
cout << "\n\n\nFor the sake of simplicity, the population quantity has\n";
cout << "been rounded to a whole number\n\n";
return 0;
}
1) If the user enters garbage at any time (like, for example 'fgadl' instead of a number), your program
will go into an infinite loop. Does your instructor expect you to handle this case?
2) Should population_size be a float or an integral type? Although the output you display is
integral for the population size, the number you use internally to perform calculations is
floating point.
3) On day #1, should the population size be equal to what the user entered as the initial population?
Or are you saying that that is day #0? (In which case I'd suggest outputting day 0 as well).
I would recommend the percentage be entered as an actual percentage, since that is what you're asking for. ie. 10 = 10%, 100 = 100%, etc.
Then just divide that value by 100 in your calculations. Asking for a percentage but then requesting it be entered in the form of a multiplier is confusing and inconsistent.
User input, I try to treat similar to function returns, one common input call. You're using two separate input calls with your validation. One method of avoiding doing that would be like:
1 2 3 4 5 6 7 8
cout << "What is the starting size of the population\n";
//if the user enters a number less than 2, ask for a number larger than 1
do
{
cout << "(Please specify a whole number greater than 1)\n";
cin >> population_size;
}while(population_size < 2);
One prompt for information. One declaration of boundary conditions. One user input call. One check for boundary condtions.
Although, in that particular example you specify whole numbers as a condition, but don't check for it.
first off
---------------------------------------------------------------------------------------------------------------------
jsmith,
1) My professor for my previous class never complained about that, however, I'm not sure if my current professor would mind, but it has bugged me knowing that is it is possible for me to enter garbage and the program goes hay-wire, some suggestions on how to prevent that would be great if you could.
2)I would prefer that the population_size be an integer number, but I wasn't sure how to necessarily limit the users to just int numbers, and since the percent increase in the population would have to be represented as a float I assume (since its decimals) I wasn't really excited about the idea of having a decimal as a population size(345.6758), so I was trying to find a way to keep the number at least rounded to a whole. But, as I have said, im not sure of a more effective way of going about this.
3)On day 1, I wanted the value that the user entered, this being the first day before the population would begin to grow, I was just trying to avoid having a day zero, but I think your suggestion is better.
Thanks, I can't believe I forgot about that, I was really bothered by the way I asked for that input, but that little bit of advice should really help me.
good eye on the input call, I'm just a beginner so I'm always looking for ways to clean up and improve my code.
The general gist is the if() check sees if the stream had an error on the previous operation
(reading the int). It will if it couldn't read an int because of garbage in the stream. On
error, the ignore() tells the stream essentially to eat any characters in the stream up to
and including the next newline, but not to eat more than 1000 characters in any case.