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 advanceCode:/* 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; }



LinkBack URL
About LinkBacks


