Scope And Parameter Passing

This is a discussion on Scope And Parameter Passing within the C Programming forums, part of the General Programming Boards category; essentially what im trying to do is to use one print-to-file function in two different ways. i have a print-to-error-file ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Unhappy Scope And Parameter Passing

    essentially what im trying to do is to use one print-to-file function in two different ways. i have a print-to-error-file and a print-to-correct-file after i check some user input and i want to combine them.

    i am having some problems when i pass in the parameters. here is what i pass:
    1. BOOK *fptr as fptr, the pointer to the struct.
    2. int num as goods or bads, a seperate counter to print to each seperate file (1. line one, 2. line two, etc.)
    3. char *fil as good_file or bad_file for the file to print to
    4. a as a for the 1st number of the multidimensional array to print

    plz take a look and give me some ideas, thanks.

    DECLARATIONS:
    Code:
    void printer(BOOK *fptr, int num, char *fil, int a);
    FILE *good_file = NULL;
    FILE *bad_file = NULL;
    int s1 = 0, s2 = 0, s3 = 0; /* String length of read in lines */
    CALL TO FUNCTION:
    Code:
    void automatic(BOOK *fptr)
    {
       int goods = 0, bads = 0, a = 0;
       good_file = fopen("address book.txt", "w");
       bad_file = fopen("errors.txt", "w");
       ...
       ...
       if(good)
       {   printer(fptr, goods, good_file, a);   }
       else
       {   printer(fptr, bads, bad_file, a);   }
    }
    FUNCTION:
    Code:
    void printer(BOOK *fptr, int num, char *fil, int a)
    {
       int l, m, n, o, p, q, r;
       num++;                                 /* Increment correct count for print out */
       fprintf(fil, "%d. ", num);       /* Print line number to file */
       for(l = 0; l < UNIT_MAX_SIZE; l++)       /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->flat_unit[a][l]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " ");                 /* Print space to file */
       for(m = 0; m < NUMBER_MAX_SIZE; m++)     /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->flat_number[a][m]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " %c ", fptr->flat_divider[a]); /* Print character to file */
       for(n = 0; n < NUMBER_MAX_SIZE; n++)     /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->number[a][n]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " ");                 /* Print space to file */
       for(o = 0; o < s1; o++)                  /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->street_name[a][o]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " ");                 /* Print space to file */
       for(p = 0; p < s2; p++)                  /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->street_type[a][p]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " ");                 /* Print space to file */
       for(q = 0; q < s3; q++)                  /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->suburb_name[a][q]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, " ");                 /* Print space to file */
       for(r = 0; r < POSTCODE_MAX_SIZE; r++)   /* For loop to print whole string to file */
       {                                        /* Start for loop */
          fprintf(fil, "%c", fptr->postcode[a][r]); /* Print character to file */
       }                                        /* End for loop */
       fprintf(fil, "\n");                /* Print end of line to file */
    }
    COMPILE ERRORS:
    (there are more above this but my ms-dos prompt scrolls through them to fast - but they are pretty much similar because of the same fprintf calls)

    assign.c(406) : Error: need explicit cast for function parameter 3 to get
    from: struct _iobuf*
    to : char *
    fprintf(fil, "%d. ", num);
    ^
    assign.c(495) : Error: need explicit cast for function parameter 1 to get
    from: char *
    to : struct _iobuf*
    fprintf(fil, "%c", fptr->flat_unit[a][l]);
    ^
    assign.c(498) : Error: need explicit cast for function parameter 1 to get
    from: char *
    to : struct _iobuf*
    fprintf(fil, " ");
    ^
    assign.c(500) : Error: need explicit cast for function parameter 1 to get
    Fatal error: too many errors
    --- errorlevel 1


    so overall this function is pretty screwed i know. but if someone has any ideas that would be great.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void printer(BOOK *fptr, int num, char *fil, int a)

    You are passing a FILE structure for the third parameter (good_file or bad_file), so change this to:
    void printer(BOOK *fptr, int num, FILE *fil, int a)

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    great thanks swoopy, ive fixed it now.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Question Got Another One For Passing Parameters

    ok what im trying to do is to combine 3 bits of code that are exactly the same except for what the read in character is passed into.

    firstly i read in from the file to a temp 'z', then do some checking which is all the same, and then i want to move it into the struct (but i want it to go to 3 different multidimensional arrays to store).

    i cant figure out what to pass and how so to put the 1st in 'street_name', 2nd in 'street_type', & 3rd in 'suburb_name'.

    check out my code and help if u can, thanx.

    DECLARATION:
    Code:
    void readString(BOOK *fptr, char z, FILE *bfil, int a);
    CALL TO FUNCTION:
    Code:
    readString(fptr, z, batch_in_file, a);
    FUNCTION:
    Code:
    void readString(BOOK *fptr, char z, FILE *bfil, int a)
    {
       int b = 0;                                        /* Declare temporary counting variables */
       count = 0;                                        /* Reset counter to read incoming string */
       rester();                                         /* Call to reset word test variables */
       for (b = 0; b < STRING_MAX_SIZE; b++)             /* Run loop until maximum string size */
       {                                                 /* Start for loop */
          if(ended == TRUE)                              /* Check if end of word has been reached */
          {                                              /* Start if statement */
             break;                                      /* Break from for loop if at end of word */
          }                                              /* End if statement */
          z = fgetc(bfil);                               /* Gather character from file to temporary variable */
          if((z == ' ') && (started == FALSE))           /* Check if read in character is a space */
          {                                              /* Start if statement */
             b--;                                        /* Reset counter back one to allow space */
          }                                              /* End if statement */
          else                                           /* Else read in character is not a space */
          {                                              /* Start else statement */
             started = TRUE;                             /* Set started to true if word has begun */
             if((z == ' ') && (started == TRUE))         /* Check if character is a space and word has started */
             {                                           /* Start if statement */
                ended = TRUE;                            /* Set end to true if at end of word */
             }                                           /* End if statement */
             else                                        /* Else character is letter in word */
             {                                           /* Start else statement */
                count++;                                 /* Add one to string size counter */
                [B]fptr->street_name[a] = z;             /* Move read in character into struct */
             }                                           /* End else statement */
          }                                              /* End else statement */
       }                                                 /* End for loop */
    }
    what the heck do i put in the bolded bits so i can just call it and pass in where i want it to go. thanx again.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I may not understand your question completely, but if not, post back and let me know. First off, you can make variable z local to the function. Then if I understand, use a if() or switch() to store your data into three different elements.
    Code:
    void readString(BOOK *fptr, FILE *bfil, int a)
    {
       char z;
       int b = 0;                                        /* Declare temporary counting variables */
       count = 0;                                        /* Reset counter to read incoming string */
       rester();                                         /* Call to reset word test variables */
       for (b = 0; b < STRING_MAX_SIZE; b++)             /* Run loop until maximum string size */
       {                                                 /* Start for loop */
          if(ended == TRUE)                              /* Check if end of word has been reached */
          {                                              /* Start if statement */
             break;                                      /* Break from for loop if at end of word */
          }                                              /* End if statement */
          z = fgetc(bfil);                               /* Gather character from file to temporary variable */
          if((z == ' ') && (started == FALSE))           /* Check if read in character is a space */
          {                                              /* Start if statement */
             b--;                                        /* Reset counter back one to allow space */
          }                                              /* End if statement */
          else                                           /* Else read in character is not a space */
          {                                              /* Start else statement */
             started = TRUE;                             /* Set started to true if word has begun */
             if((z == ' ') && (started == TRUE))         /* Check if character is a space and word has started */
             {                                           /* Start if statement */
                ended = TRUE;                            /* Set end to true if at end of word */
             }                                           /* End if statement */
             else                                        /* Else character is letter in word */
             {                                           /* Start else statement */
                count++;                                 /* Add one to string size counter */
                switch (a)
                {
                   case 0:
                      fptr->street_name[b] = z;             /* Move read in character into struct */
                      break;
                   case 1:
                      fptr->street_type[b] = z;             /* Move read in character into struct */
                      break;
                   case 2:
                      fptr->suburb_name[b] = z;             /* Move read in character into struct */
                      break;
                }
             }                                           /* End else statement */
          }                                              /* End else statement */
       }                                                 /* End for loop */
    }
    Last edited by swoopy; 03-28-2005 at 07:49 AM.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also, I think z should really be declared an int, as that's what fgetc() returns. The reason being, you can't ever check for EOF if it's declared a char.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    ok thanks again for ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 02:17 AM
  2. Replies: 2
    Last Post: 07-03-2008, 12:31 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Passing file stream as function parameter
    By simonc2 in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2004, 12:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21