Thread: structures & scanf

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

    structures & scanf

    Hi...newbie here, finished a beginner C book a few months back and am beginning to pick it up. Here is part of my first program, just a small part of if, i ready "A Development Process" on this web site.

    Just curious how, i usually include my own header in it's own file, just wondering how to use scanf onto a variable or character string then spit it back out on the screen using puts or printf. Thanks.
    Attached Files Attached Files
    Last edited by _jamie; 05-29-2018 at 12:16 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,117
    Well you have an array of struct, so you need
    var[subscript].member
    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
    Feb 2018
    Location
    San Diego, CA
    Posts
    53
    Code:
    // Program to log weight to text file
    // 	written by Jamie Morrissey 5/30/18
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct myStruct {
    	int day;
    	int month;
    	int year;
    	float pounds;
    };
    
    
    FILE * fptr;
    
    
    main()
    {
    	struct myStruct weight[5];
    
    
    	fptr = fopen("my_weight.txt", "a+");
    
    
    	if(fptr == 0)
    	{
    		printf("Error! File not found!\n");
    		exit (1);
    	}
    
    
    	int i;
    
    
    	for(i=0;i<1;i++)
    	{
    		printf("Enter date (MM/DD/YY): ");
    		scanf("%d/%d/%d", &weight[i].month, &weight[i].day, &weight[i].year);
    		printf("Enter your weight in pounds: ");
    		scanf("%f", &weight[i].pounds);
    	}
    
    
    	fprintf(fptr, "Here is your weight: ");
    
    
    	//	fseek();
    	for(i=0;i<1;i++)
    	{
    		fprintf(fptr, "%d/%d/%d - %.1f\n", weight[i].month, weight[i].day, weight[i].year, weight[i].pounds);
    	}
    
    
    	fclose(fptr);
    	return 0;
    }
    Well, here's the final program, i gave up on the function part and didn't use another function besides main(). I am happy i am picking it up after reading a book for about 4 months.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some issues with if else in structures also scanf
    By May-En Liu in forum C Programming
    Replies: 4
    Last Post: 09-17-2015, 08:11 AM
  2. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Accessing structures contained in structures
    By enigmaatj in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:53 AM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread