Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# C++ loop issues

### #1 Sparklies Posted 03 October 2006 - 09:08 AM

Sparklies

Member

• Member
• 11 posts

Hi all.

I am writing a program for C++ that should predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily poulation increase (as a percentage) and the number of days they will multiply. A loop should display the population for each day.

I am a very confused girlie and could REALLY use some help.

This is what I have so far:

```#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
int orgs, avg, days, percent;

//Ask User for # of Organisms
cout << "Enter the starting number of organisms: "
<< orgs	<< endl;
cin  >> orgs

//Ask User for % Poulation Increase
cout << "Enter the average daily population incease (as a %): "
<< percent << endl;
avg = (percent/100)
cin  >> percent

//Ask User for # Days to Multiply
cout >> "Enter the number of days to multiply: \n\n\n"
<< days   << endl;

cout << "		  Population Chart\n\n"
cout << "======================================\n\n"
cout << " DAY #						Population\n"

while (days++ avg)
cout <<

```

Thanks
• 0

### #2 MaverickSidewinder Posted 04 October 2006 - 03:07 PM

MaverickSidewinder

Member

• Member
• 257 posts
Try putting the loop before the output section...
• 0

### #3 Sparklies Posted 04 October 2006 - 03:31 PM

Sparklies

Member

• Topic Starter
• Member
• 11 posts
how would that be written out?
• 0

### #4 MaverickSidewinder Posted 07 October 2006 - 08:59 AM

MaverickSidewinder

Member

• Member
• 257 posts
```#include <iostream>

#include <iomanip>

#include <cmath>

using namespace std;

int main ()

{

int orgs, avg, days, percent;

//Ask User for # of Organisms

cout << "Enter the starting number of organisms: "

<< orgs	<< endl;

cin  >> orgs

//Ask User for % Poulation Increase

cout << "Enter the average daily population incease (as a %): "

<< percent << endl;

avg = (percent/100)

cin  >> percent

//Ask User for # Days to Multiply

cout >> "Enter the number of days to multiply: \n\n\n"

<< days   << endl;

while (days++ avg)

cout <<

cout << "		  Population Chart\n\n"

cout << "======================================\n\n"

cout << " DAY #						Population\n"

```

• 0

### #5 quim88 Posted 13 October 2006 - 11:48 AM

quim88

Member

• Member
• 32 posts
WOW!
this code is full of error you have alot things to fix here
• 0

### #6 IO-error Posted 16 October 2006 - 01:42 AM

IO-error

Member

• Member
• 276 posts
Like what errors, help him out if you see his problems.
• 0

### #7 quim88 Posted 16 October 2006 - 08:26 AM

quim88

Member

• Member
• 32 posts
There are Errors in both Code but i am just going to point out the errors in the first.

```#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
int orgs, avg, days, percent;

//Ask User for # of Organisms
cout << "Enter the starting number of organisms: "
<< orgs	<<endl;
cin  >> orgs```

you defined the int orgs but you cannot output it yet becouse it has no value
the code should be like this->
```#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
int orgs, avg, days, percent;

//Ask User for # of Organisms
cout << "Enter the starting number of organisms: "<<endl;
cin  >> orgs```

you did the same error percent does not have a value yet so it cannot be out put

```//Ask User for % Poulation Increase
cout << "Enter the average daily population incease (as a %): "
<< percent << endl;
avg = (percent/100)
cin  >> percent```

it should be like this:
```//Ask User for % Poulation Increase
cout << "Enter the average daily population incease (as a %): "<< endl;
avg = (percent/100)
cin  >> percent```

Same error here i am not going to go over it and the cout >> should be cout <<
```//Ask User for # Days to Multiply
cout >> "Enter the number of days to multiply: \n\n\n"
<< days << endl;```

another Error that you didn't and it was to many for me to go over is that you forgot to end your code line with ;
• 0

### #8 Sparklies Posted 16 October 2006 - 09:19 AM

Sparklies

Member

• Topic Starter
• Member
• 11 posts
Thanks for all of the help.

It is definitely appreciated that you showed me what I did wrong rather than just say "HERE".

I will go back over my code and fix the end of statement tags and the other stuff.
• 0

### #9 Granz00 Posted 19 October 2006 - 01:15 AM

Granz00

Member

• Member
• 226 posts
```#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
int orgs, avg, days, percent;

//Ask User for # of Organisms
cout << "Enter the starting number of organisms:  ";
cin  >> orgs

//Ask User for % Poulation Increase
cout << "Enter the average daily population incease (as a %):  ";
cin >> percent;
avg = (percent/100);

//Ask User for # Days to Multiply
cout << "Enter the number of days to multiply:  ";
cin >> days;

cout << "		  Population Chart\n\n";
cout << "======================================\n\n";
cout << " DAY #			Population\n";

//Start at 0 for your initial population
t = 0;

//Will display initial population plus up to your requested amount of days
while (days >= t)
{
cout << t << "				" << orgs << endl;
orgs = (orgs * avg) + orgs;
t++;
}

return 0;
}
```

I added in the variable t for use in your loop. (Think about why i needed this along with days)
I wrote the code with the mistakes fixed. (Hopefully)
I wrote a loop for what seemed to be the desired output. (Minus how it appears on screen)
One mistake that was not caught by the others is;
avg = (percent/100); will not calculate what you want it to, unless you input percent before hand.
Also, it is good programming practice to initialize all of your variables when you make them.
Ex.
int orgs = 0, avg = 0, days = 0, percent = 0, t = 0;

Edited by Granz00, 19 October 2006 - 01:19 AM.

• 0

### #10 Sparklies Posted 25 October 2006 - 03:21 PM

Sparklies

Member

• Topic Starter
• Member
• 11 posts
Thanks for all of your help everyone
• 0

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users