help with c

This is a discussion on help with c within the C Programming forums, part of the General Programming Boards category; Code: /* This program expects the name of an input file and an output file to be given by the ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    help with c

    Code:
    /* This program expects the name of an input file and an output
    file to be given by the user.
          Name : Dang Vo
    	  Date : 04/14/11
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define NUM_LINE 21
    
    typedef struct
    {
    	char states[50];
    	char cities[50];
    	int tempt;
    } INFO;
    int getData(INFO list[]);
    void insertionSort(INFO list[], int last);
    int printData(INFO list[]);
    int main (void)
    {
    
    // Local Declarations
    
    INFO list[NUM_LINE];
    int last = 1;
    
    // Statements
    getData(list);
    insertionSort(list, last);
    printData(list);
    return 0;
    }
    
    /*========================== getData========================
    Pre: FILE *spData
    Post: FILE *spData
    */
    // Local Declarations
    
    
    int getData(INFO list[])
    {
    // Local declarations
    FILE *spData;
    char states[50];
    char cities[50];
    int tempt;
    
    //open file
    spData = fopen("in.txt", "r");
    
    if (spData != NULL)
    {
    fgets(states, 50, spData);
    fgets(cities, 50, spData);
    scanf( "%d", &tempt );
    printf("Sucessfully open in.txt\n");
    fclose(spData);
    }
    else
    {
    	printf("Error open in.txt\n");
    }
    return 0;
    }
    
    /*==============================insertionSort ================================
    Pre: unsorted info
    Post: sorted info
    */
    void insertionSort(INFO list[], int last)
    {
    // Local Declarations
       bool located;
       INFO temp;
       INFO *pCurrent;
       INFO *pWalker;
       INFO *pLast;
    
    // Statements
       for (pCurrent = list + 1, pLast = list + last;
    	   pCurrent <= pLast;
    	   pCurrent++)
       {
        located = false;
    	temp    = *pCurrent;
    
    	for (pWalker  = pCurrent - 1;
    		 pWalker >= list && !located;)
          if (strcmp(temp.states, pWalker->states) < 0)
    	     {
              *(pWalker + 1) = *pWalker;
    		  pWalker--;
    	     }
    	  else
             located = true;
    	*(pWalker + 1) = temp;
       }
    
    return;
    }
    
    
    /*=============================== printData ================================
    Pre: unsorted list
    POst:sorted  list, average temperature
    */
    int printData(INFO list[])
    {
        FILE *spData;
    char states[50], cities[50];
    int tempt;
    
    //open file
    spData = fopen("in.txt", "a");
    
    if (spData != NULL)
    {
    	fprintf(spData,"%s %s %d", states, cities, &tempt);
    
    printf("Sucessfully open in.txt\n");
    fclose(spData);
    }
    else
    {
    	printf("Error openning in.txt\n");
    }
    return 0;
    }
    my program is printing garbage how do i set value to states, cities, tempt. the program kinda messy now so i don't really see how to do it, should have initialize them at the beginning though. thank in advance
    Attached Files Attached Files

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    You could fix getdata, like I suggested in one of your other very similar threads on the same topic.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Oh nos! Third thread. Time to press the ban button.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    Any chance you could maybe use a subject other than "help with c"? Or is that too much to ask?

Popular pages Recent additions subscribe to a feed

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