program not working...please look at this
hello everybody. if some of you have seen some of my posts, you'll know that i'm making an ecosystem simulation. it includes three animals, with a ?/? chance that an animal will be created or destroyed. the information is outputed to a log which i then analyze to see any patterns....(lol, aren't i nerd :p ). two problems:
1. after the first animal, logs are created, but there is nothing in them....?!?!
2. after the third animal creation (carnivore creation), the program ternimates...!?!!?
i started c and C++ about two weeks ago, and i have learned a ton of stuff over that time, but seeing as even people that ahve been programming for years have problems, i will have to say i'm quite a newbie. i've looked quite a bit, but i can't find anything wrong with the code. please help:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
float numerator;
float denominator;
int interval;
int created = 0;
int total = 0;
int end;
int sample;
FILE *fp;
srand (time(0));
printf("Welcome to Eco-Sim!\n");
printf("Version 1.00\n");
printf("By Jack Olszewski\n");
printf("\n");
printf ("Time (1 = 1 Year):");
scanf ("%d", &end);
printf("\n\n");
printf("Plant:\n");
printf("\n");
printf ("Enter the Numerator for Creation: ");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Creation: ");
scanf ("%f", &denominator);
printf ("\nProbability Of Creation Is: %f\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("plantcreation.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end)
{
sample = rand ();
fprintf(fp,"Plant Creation Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Created\n\n");
created++;
}
else
fprintf (fp, "Not Created\n\n");
total++;
}
printf("\n");
printf ("Enter the Numerator for Destruction: ");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Destruction: ");
scanf ("%f", &denominator);
printf ("\nProbability Of Destruction Is: %f\n\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("plantdestruction.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end)
{
sample = rand ();
fprintf (fp,"Plant Destruction Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Destroyed\n\n");
created++;
}
else
fprintf (fp, "Not Destroyed\n\n");
total++;
printf("Herbivore:");
printf("\n\n");
printf ("Enter the Numerator for Creation: ");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Creation: ");
scanf ("%f", &denominator);
printf ("\nProbability Of Creation Is: %f\n\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("herbivorecreation.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end)
{
sample = rand ();
fprintf (fp,"Herbivore Creation Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Created\n\n");
created++;
}
else
fprintf (fp, "Not Created\n\n");
total++;
}
printf ("Enter the Numerator for Destruction: ");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Destruction: ");
scanf ("%f", &denominator);
printf ("\nProbability Of Destruction Is: %f\n\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("herbivoredestruction.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end)
{
sample = rand ();
fprintf (fp,"Herbivore Destruction Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Destroyed\n\n");
created++;
}
else
fprintf (fp, "Not Destroyed\n\n");
total++;
}
printf("Carnivore:");
printf("\n\n");
printf ("Enter the Numerator for Creation:");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Creation:");
scanf ("%f", &denominator);
printf ("\nProbability Of Creation Is: %f\n\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("carnivorecreation.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end);
{
sample = rand ();
fprintf (fp,"Carnivore Creation Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Created\n\n");
created++;
}
else
fprintf (fp, "Not Created\n\n");
total++;
}
printf ("Enter the Numerator for Destruction: ");
scanf ("%f", &numerator);
printf ("Enter the Denominator for Destruction: ");
scanf ("%f", &denominator);
printf ("\nProbability Of Destruction Is: %f\n\n", numerator/denominator);
interval = (int)((numerator/denominator)*RAND_MAX);
if ((fp = fopen ("carnivoredestruction.txt", "w")) == NULL)
{
printf ("\nError: Unable to open file");
return 1;
}
end = end + time (0);
while (time (0) < end);
{
sample = rand ();
fprintf (fp,"Carnivore Destruction Log");
fprintf (fp, "Day: %d\t", total);
if (sample < interval)
{
fprintf (fp, "Destroyed\n\n");
created++;
}
else
fprintf (fp, "Not Destroyed\n\n");
total++;
}
return 0;
}
thanks in advance,
jolszewski