Hint Need For Assignment Due Tonight! How to pick out an item while in the loop and nothing else
Sep 30, 2012 at 9:58pm UTC
Hello and thank you in advance for any help. I'm not asking for anyone to do it for me, but some direction would really help.
I have two questions
1. How do I get the numbers not to wrap in the console after outputting?
2. How to select when the the year that population doubles?
Problem:
Write a program that allows you to enter the current population, and the constant rate of increase over the years. Calculate and display the results over 75 years. Then show what year it doubled.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int yearcounter = 1;
double Currentpop;
int Startingpop;
double Populationgrowth;
double Populationtotal;
int Doublepop;
int Growthrate;
//Enter starting population
{
cout << "Enter Current Population: " ; //prompt for input
cin >> Startingpop;
}
//Enter population growth rate
{
cout << "Enter Population Growth Rate: " ; //prompt for intput
cin >> Growthrate;
}
//titles
{
cout << "Year\t" "Population\t" "Population Change\t" "\n" ;
}
//Making starting pop into current pop
Currentpop = Startingpop;
Doublepop = Startingpop*2;
while (yearcounter <= 75)
{
Populationgrowth = (static_cast <double >(Currentpop)*Growthrate);
cout << yearcounter << "\t" << static_cast <double > (Currentpop)<<setprecision(0) <<fixed << "\t\t" << Populationgrowth<<setprecision(0)<<fixed<< "\n" ;
Populationtotal = static_cast <double >(Populationgrowth) + (Currentpop);
Currentpop = static_cast <double >(Populationtotal);
yearcounter = yearcounter + 1; //increment by 1
}
{
if (Doublepop >= Currentpop)
cout << "The year it doubled is" <<yearcounter;
}
}
Sep 30, 2012 at 11:43pm UTC
added
static int thisisit;
.
In your while loop, added
1 2 3 4
if (Doublepop >= Currentpop)
{
thisisit = yearcounter;
}
Following the loop, added
cout << "The year it doubled is " << thisisit+1;
Remove if statement following the loop. When I compiled it, I had to add +1 to thisisit. Not sure why. I didn't examine all of your code thoroughly.
Oct 1, 2012 at 12:06am UTC
Thanks for the step forward!
How do I get it from not putting "the year it doubled is" after each of the years and just at the end with the result?
Oct 1, 2012 at 2:18am UTC
I figured it out, but would still love how to get the numbers to format right without all the trailing zeros, but I'm going to go ahead and send it in as is.
Thank you again for the help!
Oct 1, 2012 at 2:48am UTC
Trailing zeroes? It's probably got to do with your "setprecision".
Topic archived. No new replies allowed.