Hi i got this progamm working fine
it is supposed to read a comma delimited text file store as an array and the output a formatted version
The problem is its outputting something really weird I dont know why.
The text file it is supposed to read
Darwin,3,33
Darwin,1,11
Darwin,6,66
Darwin,4,44
Darwin,5,55
Brisbane,2,222
Brisbane,1,111
Brisbane,6,666
Brisbane,3,333
Sydney,1,101
Sydney,2,202
Sydney,3,303
Sydney,6,606
Sydney,5,505
Sydney,4,404
Perth,3,313
Perth,4,414
Perth,5,515
THE CODE
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
char *fname,*str,ch,cityname[5][12];
int monthnum,rainfall,pos,monthvalues[5][6],flag,i,j,cpos,npos=0,count=0;
FILE *fp; //File Pointer
fp=fopen("CityRain.txt","r+");
if(fp==NULL)
{
printf("Cannot input file\n");
getch();
return 0;
}
for(i=0;i<5;i++)
strcpy(cityname[i],"");
for(i=0;i<5;i++)
for(j=0;j<6;j++)
monthvalues[i][j]=0;
while(!feof(fp))
{
pos=0;
flag=1;
str = (char* ) malloc(12 * sizeof(char) ); //dynamic memory allocation
while(flag)
{
fscanf(fp,"%c",&ch);
// ch=fgetc(fp);
if(ch==',')
{
flag=0;
}
else
{
str[pos]=ch;
pos++;
}
}
cpos=-1;
for(i=0;i<5;i++)
{
if(strcmp(str,cityname[i])==0)
cpos=i;
}
if(cpos==-1)
{
strcpy(cityname[npos],str);
npos++;
}
fscanf(fp,"%d",&monthnum);
fscanf(fp,"%c",&ch);
fscanf(fp,"%d",&rainfall);
if(npos==1 || npos==2)
monthvalues[1][monthnum-1]=rainfall;
else
monthvalues[npos-1][monthnum-1]=rainfall;
count++;
}
printf("City Jan Feb Mar Apr Jun\n");
for(i=1;i<5;i++)
{
printf("%s\t",cityname[i]);
if(i!=2)printf("\t");
for(j=0;j<6;j++)
printf("%d\t",monthvalues[i][j]);
printf("\n");
}
getch();
return 0;
}
Am supposed to get a better looking output
But this is what I get
OUTPUT
City Jan Feb Mar Apr Jun
Darwin═════
Brisbane═══
Sydney═════
Perth══════
Perth═════,
Perth══════²²²²½½½½½½½½ 11 0 33 44 55 66
Brisbane═══
Sydney═════
Perth══════
Perth═════,
Perth══════²²²²½½½½½½½½ 111 222 333 0 0 666
Sydney═════
Perth══════
Perth═════,
Perth══════²²²²½½½½½½½½ 101 202 303 404 505 606
Perth══════
Perth═════,
Perth══════²²²²½½½½½½½½ 0 0 313 0 0 0
Please tell me where I am wrong, because am getting these wierd symbols