What am I doing wrong?
Hello! I really hope some kind, capable soul(s) can help me out here! I am trying to write a program that reads a data file, each line of which has a character followed by some weather data. The character determines what action to take with the data. So I have a fairly complicated switch statement inside a while (fscanf(stuff)!=EOF) loop.
The program DOES compile, but for some reason, the ouput is twice as big as the number of lines in the data file. Also, I can't get the 'a' and 'p' cases to execute. The other cases, except default, only execute partially.
And here is the data I'm using.
P xxx 0.0 0 xx
p Baltimore 0.0 0 NW
L Fredricksburg -28.4 10 SW
H Death_Valley 132.7 8 NE
F Boulder 75.4 67 W
a Boulder 75.4 67 W
P Grants 56.3 29 SW
a Denver 70.3 23 S
a Albuquerque 72.3 12 NW
a Chicago 65.9 34 NE
P xxx 0.0 0 xx
p Denver 0.0 0 NW
L Fredricksburg -28.4 10 SW
H Death_Valley 132.7 8 NE
F Boulder 75.4 67 W
a Detroit 65.9 12 NW
L xxx 0.0 0 xx
a Loveland 73.2 67 W
F sdja 0.1 3 hh
a Albuquerque 71.2 12 SE
P asd 1.2 1 we
p New_York 0.0 0 ss
ANY help would be appreciated. :)
The program DOES compile, but for some reason, the ouput is twice as big as the number of lines in the data file. Also, I can't get the 'a' and 'p' cases to execute. The other cases, except default, only execute partially.
#include <stdio.h>
/*Set up structure*/
typedef struct
{
char cityname[41];
float temperature;
int windspd;
char winddir[3];
}citydata;
/*Function Prototypes*/
int maxtemp(citydata [], int);
int mintemp(citydata [], int);
int maxwind(citydata [], int);
int main(int argc, char *argv[])
{
/*Definitions of Variables*/
citydata city[30];
char action;
char tempcity[41];
float temptemp=0.0;
int tempwind=0;
char tempdir[3];
int citycount=0;
FILE *inputfile;
int i=0;
int j=0;
int index=0;
/*Make sure of correct # of parameters. If
incorrect, print error msg.*/
if(argc != 2)
{
printf("Wrong # of parameters!\n");
printf("Usage: %s <input-file-name>\n", argv[0]);
exit(-1);
}
/*Make sure file DOES open. If not, print error msg.*/
inputfile=fopen(argv[1], "r");
if((inputfile = fopen(argv[1], "r"))==NULL)
{
printf("Unable to open file %s for input\n", argv[1]);
exit(-1);
}
while(fscanf(inputfile, "%c %s %f %d %s", &action, tempcity, &temptemp, &tempwind, tempdir) != EOF)
{
switch(action)
{
case 'a': /*make sure temp city isn't a duplicate*/
for(j=0; j<=citycount; j++)
{
if(strcmp(city[j].cityname, tempcity)
==0)
{
printf("Duplicate city %s\n", tempcity);
break;
}
}
/*if not duplicate, put temp info into structure*/
strcpy(city[i].cityname, tempcity);
city[i].temperature=temptemp;
city[i].windspd=tempwind;
strcpy(city[i].winddir, tempdir);
/*increase citycount by 1*/
citycount++;
break;
case 'p': if(citycount == 0)
{
printf("No cities to print p !\n");
break;
}
/*make sure city DOES exist in array*/
for(j=0; j<citycount; j++)
{
if (strcmp(city[j].cityname, tempcity)
==0)
/*then print the data for it*/
{
/*print city's data*/
printf("City \t \t Temp \t WindSpeed \t Direction\n");
printf("%s \t \t %f \t %i \t %s\n", city[j].cityname,
city[j].temperature, city[j].windspd, city[j].winddir);
break;
}
}
/*if city not found, print error msg*/
printf("%s not found\n", tempcity);
break;
case 'P': if(citycount == 0)
{
printf("No cities to print P !\n");
break;
}
/*then print data for ALL cities*/
for(j=0; j<citycount; j++)
{
printf("City \t \t Temp \t WindSpeed \t Direction\n");
printf("%s \t \t %f \t %i \t %s\n", city[j].cityname,
city[j].temperature, city[j].windspd, city[j].winddir);
}
break;
case 'L': if(citycount == 0)
{
printf("No cities to print L !\n");
break;
}
/*if two cities have lowest temp, print both*/
/*otherwise, print city info for lowest temp*/
index=mintemp(city, citycount);
printf("Lowest temp is %f in %s\n", city[index].temperature, city[index].cityname);
break;
case 'H': if (citycount == 0)
{
printf("No cities to print H !\n");
break;
}
/*find highest temperature*/
index=maxtemp(city, citycount);
printf("Highest temp is %f in %s\n", city[index].temperature, city[index].cityname);
break;
case 'F': if(citycount == 0)
{
printf("No cities to print F !\n");
break;
}
index=maxwind(city, citycount);
printf("Fastest wind speed is %i in %s", city[index].windspd, city[index].cityname);
break;
default: printf("Invalid action character!\n");
printf("Usgae: a, p, P, L, H, F\n");
break;
}
i++;
}
/*Close input file*/
fclose(inputfile);
return(0);
}
/**********************************************************
Function definitions
**********************************************************/
int maxtemp(citydata c[], int size)
{
int hold=0;
int i;
for (i=1; i<size; i++)
{
//If value in current structure is bigger than
//value being held...
if (c[i].temperature > c[hold].temperature)
{
//...hold index of bigger value
hold=i;
}
}
return hold;
}
int mintemp(citydata c[], int size)
{
int hold=0;
int i;
for (i=1; i<size; i++)
{
//If value in current structure is smaller than
//value being held...
if (c[i].temperature < c[hold].temperature)
{
//...hold index of smaller value
hold=i;
}
}
return hold;
}
int maxwind(citydata c[], int size)
{
int hold=0;
int i;
for (i=1; i<size; i++)
{
//If value in current structure is bigger than
//value being held...
if (c[i].windspd > c[hold].windspd)
{
//...hold index of bigger value
hold=i;
}
}
return hold;
}
And here is the data I'm using.
P xxx 0.0 0 xx
p Baltimore 0.0 0 NW
L Fredricksburg -28.4 10 SW
H Death_Valley 132.7 8 NE
F Boulder 75.4 67 W
a Boulder 75.4 67 W
P Grants 56.3 29 SW
a Denver 70.3 23 S
a Albuquerque 72.3 12 NW
a Chicago 65.9 34 NE
P xxx 0.0 0 xx
p Denver 0.0 0 NW
L Fredricksburg -28.4 10 SW
H Death_Valley 132.7 8 NE
F Boulder 75.4 67 W
a Detroit 65.9 12 NW
L xxx 0.0 0 xx
a Loveland 73.2 67 W
F sdja 0.1 3 hh
a Albuquerque 71.2 12 SE
P asd 1.2 1 we
p New_York 0.0 0 ss
ANY help would be appreciated. :)
