Thread: fprintf and passing functions

  1. #1
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123

    fprintf and passing functions

    Code:
    #include <stdio.h>
    
    
    struct myStruct {
        int day;
        int month;
        int year;
        float pounds;
    };
    
    
    
    
    FILE * fptr;
    
    
    // int output(int i);
    
    
    main()
    {
        fptr = fopen("my_weight.txt", "w");
    
    
        if(fptr == 0)
        {
            printf("Error--could not open file.\n");
            exit (1);
        }
    
    
        input();
        output();
    
    
        return 0;
    }
    
    
    input()
    {
        struct myStruct weight[4];
        
        int i;
    
    
        for(i=0;i<1;i++)
        {
            printf("Enter date (MM/DD/YY) ");
            scanf("%d/%d/%d", &weight[i].day, &weight[i].month, &weight[i].year);
            printf("Enter weight (in pounds): ");
            scanf("%f", &weight[i].pounds);
        }
    
    
        return;
    }
    
    
    
    
    output()
    {
        fprintf(fptr, "\n\nHere is you weight: \n");
    
    
        int i;
    
    
        for(i=0;i<1;i++)
        {
            fprintf(fptr, "%d", weight[i].day);
        }
    
    
        return;
    }
    good morning, newbie question again. I finished a book and am starting to pick it up. Just have a question with the structure "weight[i].day' for fprintf at the bottom of the line, I have read development process on this web site and start small and compile often.

    And also, was wondering what should be inside "output()" for output() function. Also questioning why line numbers aren't being showed when I do code tags. Thanks and look forward to posting more.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Declare your array in main, and pass it to both functions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Don't want to patronize but your functions must have and return a certain data-type. Or void if they return nothing.
    Like
    Code:
    int main()
    {
        ---------
        return(some_integer_value);
    }

  4. #4
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    I pasted the final program, structures &amp; scanf

    i gave up on the functions part

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Code:
    static void output(struct myStruct weight[], FILE * fptr) {
      fprintf(fptr, "\n\nHere is your weight: \n");
        for (int i = 0; i < 4; i++)
          fprintf(fptr, "%d/%d/%d,%f\n",
                  weight[i].day,
                  weight[i].month,
                  weight[i].year,
                  weight[i].pounds); }
    
    int main(void) {
      ...
        output(weight, fptr); }

  6. #6
    Registered User
    Join Date
    Feb 2018
    Location
    San Diego, CA
    Posts
    123
    how about this (this code works)

    Code:
    // weight_6.c
    // structure to function
    // weight_4.c & weight_5.c
    // written by Jamie Morrissey 6/12/18
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct data {
    	int day;
    	int month;
    	int year;
    	float pounds;
    } weight;
    
    FILE * fptr;
    
    void fprintf_stuff(struct data print_it);
    
    main()
    {
    	printf("Enter date: (MM/DD/YY): ");
    	scanf("%d/%d/%d", &weight.month, &weight.day, &weight.year);
    	printf("Enter your weight in pounds: ");
    	scanf("%f", &weight.pounds);
    
    	// call the fprint_stuff() function
    	fprintf_stuff(weight);
    
    	return 0;
    }
    
    void fprintf_stuff(struct data print_it)
    {
    	fptr = fopen("new_weight_new.txt", "a+");
    
    	if( fptr == 0)
    	{
    		printf("Error! File not found!\n");
    		exit(1);
    	}
    
    	fprintf(fptr, "Here is your weight: ");
    	fprintf(fptr, "%d/%d/%d - %.1f\n", weight.month, weight.day, weight.year, weight.pounds);
    
    	fclose(fptr);
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Better.

    Now do it without using any global variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Replies: 7
    Last Post: 03-26-2008, 03:21 AM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread