Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

C++ loop issues


  • Please log in to reply

#1
Sparklies

Sparklies

    Member

  • Member
  • PipPip
  • 11 posts
:whistling:

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 :blink:
  • 0

Advertisements


#2
MaverickSidewinder

MaverickSidewinder

    Member

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

#3
Sparklies

Sparklies

    Member

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

#4
MaverickSidewinder

MaverickSidewinder

    Member

  • Member
  • PipPipPip
  • 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

quim88

    Member

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

#6
IO-error

IO-error

    Member

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

#7
quim88

quim88

    Member

  • Member
  • PipPip
  • 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

Sparklies

    Member

  • Topic Starter
  • Member
  • PipPip
  • 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

Granz00

    Member

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

int main ()
{
int orgs, avg, days, percent;
int t; //for your loop

//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

Sparklies

    Member

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






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP