Thread: Program Help

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    7

    Program Help

    I need help figuring out how to change the below program so that its output is sent to a file.


    #include
    /*Jason Jones */
    main ()
    {
    int day_code, /* day_code = 0 means the month starts on Sun
    day_code = 1 means the month starts on Mon
    day_code = 2 means the month starts on Tues, etc */
    days_in_month, /* number of days in month currently being
    printed */
    leap_year, /* 1 means leap year; 0 means no leap year */
    day, /*counter for day of month */
    month; /* month = 1 is Jan, month =2 is Feb, etc */
    do {
    printf (“Enter day and leap year codes: “);
    scanf (“%d%d”, &day_code, &leap_year);
    } while (day_code < 0 || day_code > 6 );
    for (month = 1; month <= 12; month ++) {
    switch (month) { /* print name and set days_in_month */

    case 1:
    printf(“\n\nJanuary”);
    days_in_month = 31;
    break;
    case 2:
    printf(“\n\nFebruary”);
    days_in_month = leap_year ? 29 : 28;
    break;
    case 3:
    printf(“\n\nMarch”);
    days_in_month = 31;
    break;
    case 4:
    printf(“\n\nApril”);
    days_in_month = 30;
    break;
    case 5:
    printf(“\n\nMay”);
    days_in_month = 31;
    break;
    case 6:
    printf(“\n\nJune”);
    days_in_month = 30;
    break;
    case 7:
    printf(“\n\nJuly”);
    days_in_month = 31;
    break;
    case 8:
    printf(“\n\nAugust”);
    days_in_month = 31;
    break;
    case 9:
    printf(“\n\nSeptember”);
    days_in_month = 30;
    break;


    case 10:
    printf(“\n\nOctober”);
    days_in_month = 31;
    break;
    case 11:
    printf(“\n\nNovember”);
    days_in_month = 30;
    break;
    case 1:
    printf(“\n\nDecember”);
    days_in_month = 31;
    break;
    }
    printf( ‘\n\nSun Mon Tue Wed Thu Fri Sat\n”);
    /* advance printer to correct position for first date */
    for (day =1; day <=1 + day_code * 5; day++) {
    printf(“ “);
    /* print the dates for one month */
    for ( day = 1; day <= days_in_month; day++) {
    printf(“%2d’, day);
    if ( (day + day_code) % 7 > 0 ) /* before Sat? */
    /* move to next day in same week */
    printf(“ “);
    else /*skip to next line to start with Sun */
    printf(“\n”);
    }
    /* set day_code for next month to begin */
    day_code = (day_code + days_in_month) % 7;
    }
    }

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Instead of using printf, use fprintf and open a file.
    Code:
    FILE *outfile = fopen("FileName", "w");
    fprintf(outfile, "This is the output\n");
    //more fprints.
    ;

    or you could change stdout to the file.

    Code:
    FILE *outfile = fopen("FileName", "w");
    stdout = outfile;
    //printfs go here
    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    7
    do you have to use that line for all of the printf lines or just the one?

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    u need to open the file only once and use fprintf multiple times to write to that file

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Here's the idea:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct monthData
    {
      char *monthName;
      int monthDays;
    };
    
    static const struct monthData MD[] =
    {
      {"January",31},
      {"February",28},
      {"March",31},
      {"April",30},
      {"May",31},
      {"June",30},
      {"July",31},
      {"August",31},
      {"September",30},
      {"October",31},
      {"November",30},
      {"December",31},
    };
    
    int main ( void )
    {
      FILE *outputFile;
      int day_code = 0, 
          days_in_month = 0, 
          leap_year = 0, 
          day, 
          month;
    
      outputFile = fopen ( "dataout.txt", "w" );
      if ( outputFile == NULL ) {
        perror ( "File open error: outputFile" );
        return EXIT_FAILURE;
      }
    
      do { 
        printf ("Enter day and leap year codes: "); 
        scanf ("%d%d", &day_code, &leap_year); 
      } while (day_code < 0 || day_code > 6 );
      
      for (month = 0; month < 12; month++) {
        fprintf ( outputFile, "\n\n%s", MD[month].monthName );
        if ( month == 1 )
          /* Check for February and leap year */
          days_in_month = leap_year ? MD[month].monthDays + 1 : MD[month].monthDays;
        else
          days_in_month = MD[month].monthDays;
        /* Calculate and write one month to file */
        fprintf(outputFile, "\n\nSun Mon Tue Wed Thu Fri Sat\n");  
        for (day =1; day <= 1 + day_code * 5; day++) { 
          fprintf(outputFile, " "); 
          for ( day = 1; day <= days_in_month; day++) { 
            fprintf(outputFile, "%2d", day); 
            if ( (day + day_code) % 7 > 0 )
              fprintf(outputFile, " "); 
            else
            fprintf(outputFile, "\n"); 
          } 
          day_code = (day_code + days_in_month) % 7; 
        } 
      }
      return 0;
    }
    I also implemented a method that would make your program a bit shorter and easier to follow by removing the long switch statement. I used the option of placing the month name and number of days in an array of structs, but this method would also work with two arrays; one holding the names and one holding the days.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM