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;
    void getData(INFO list[]);
    void insertionSort(INFO list[], int last);
    //int calcAver(INFO list[]);
    void printData(INFO list[]);
    int main (void)
    {
    
    // Local Declarations
    
    //FILE *spData = fopen("in.txt", "r");
    INFO list[NUM_LINE];
    int last = 1;
    INFO states[50];
    INFO cities[50];
    INFO tempt;
    
    // Statements
    getData(list);
    insertionSort(list, last);
    calcAver(list);
    printData(list);
    return 0;
    }
    
    /*========================== getData========================
    Pre: FILE *spData
    Post: FILE *spData
    */
    // Local Declarations
    
    
    void 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("Could not open in.txt\n");
    }
    return;
    }
    
    /*==============================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
    */
    void printData(INFO list[])
    {
        FILE *spData;
    char states[50], cities[50];
    int tempt;
    
    //open file
    spData = fopen("in.txt", "w");
    
    if (spData != NULL)
    {
    	fprintf(spData,"%s %s %d", states, cities, tempt);
    
    printf("Sucessfully open in.txt\n");
    fclose(spData);
    }
    else
    {
    	printf("Could not open in.txt\n");
    }
    return;
    }
    my program is printing garbage now i know that my printData is wrong can any one suggest a way to fix it
    attachment under is the instruction and the file
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Your lack of indentation (or consistent indentation) makes it all but impossible to read the code.
    SourceForge.net: Indentation - cpwiki

    FWIW, your getdata function is only reading into local variables. It does NOT update the array you pass as a parameter.
    So it will continue to contain the same garbage it did before.

    It might be a good idea to initialise your local variables in future.
    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
    May 2009
    Posts
    2,778
    You never set a value to these variables "states, cities, tempt" and then you print them out; Why do you think it will NOT print garbage.

    Tim S.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832

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