Thread: program not working...please look at this

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    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 ). 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
    Last edited by JOlszewski; 01-30-2006 at 09:30 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You have one FILE pointer, opened once and never closed. If would be a good idea to close a file when you're done with it.

    After the first time you do this, the interval time is lost.
    Code:
       end = end + time (0);
    
       while ( time (0) < end )
    Perhaps add another variable.
    Code:
       duration = end + time (0);
       while ( time (0) < duration )
    When you notice a repeated pattern, this is your code screaming for a function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    thanks. i fixed it up, and it's all good to go.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A "something else to look at" in regard to using functions might be like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int foo(const char *name, int cd, int end)
    {
       FILE *fp;
       float numerator, denominator;
       char filename[FILENAME_MAX];
       int sample, duration, interval, created = 0, total = 0;
       static const char *CreateDestroy[] = {"Create","Destroy"};
       const char *type = CreateDestroy[cd];
    
       printf("%s:\n\n", name);
       printf ("Enter the Numerator for %s: ", type);
       scanf ("%f", &numerator);
    
       printf ("Enter the Denominator for %s: ", type);
       scanf ("%f", &denominator);
    
       printf ("\nProbability Of %s Is: %f\n", type, numerator / denominator);
    
        /*This following line sets the interval point*/
       interval = (int)((numerator / denominator) * RAND_MAX);
    
       sprintf(filename, "%s%s.txt", name, type);
       fp = fopen (filename, "w");
       if ( fp == NULL )
       {
          printf ("\nError: Unable to open file");
          return 1;
       }
    
       duration = end + time (0);
       while ( time (0) < duration )
       {
          sample = rand ();
    
          fprintf(fp,"%s %s Log", name, type);
          fprintf (fp, "Day: %d\t", total);
          if ( sample < interval )
          {
             fprintf (fp, "%s\n\n", type);
             created++;   
          }
    
          else
             fprintf (fp, "Not %s\n\n", type);
    
          total++;        
       }            
       fclose(fp);
       printf("\n");
       return 0;
    }
    
    int main ()
    {
       int end;
       srand (time(0));  
    
       puts("Welcome to Eco-Sim!");
       puts("Version 1.00");
       puts("By Jack Olszewski");
       puts("");
    
       printf ("Time (1 = 1 Year):");
       scanf ("%d", &end);
       printf("\n\n");
    
       foo("Plant", 0, end);
       foo("Plant", 1, end);
       foo("Herbivore", 0, end);
       foo("Herbivore", 1, end);
       foo("Carnivore", 0, end);
       foo("Carnivore", 1, end);
       return 0;
    }
    Quick-and-dirtyish, but just for the sake of showing something.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM