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

Recommended Answers

All 10 Replies

Those weird symbols mean you haven't terminated your strings with '\0'.

ermm ok ... where do i put that '\0' because the code is working right now i do not want to go and mess it up

also the output file is supposed to have just one city name and its stats horizontally next to it ... i don't know where I went wrong because its putting these other city names too

#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    May    Jun\n\0");
      for(i=1;i<5;i++)  
      {               
             printf("%s\t\0",cityname[i]);
             if(i!=2)printf("\t\0");
            for(j=0;j<6;j++)
                  printf("%d\t\0",monthvalues[i][j]);
             printf("\n\0");
      }
     getch();
     return 0;
}

See i have added the'\0' still dosent change

the code is working right now i do not want to go and mess it up

The code is not working right now. Right now the behavior is undefined because you're walking off the end of allocated memory. At a glance, you should terminate str before copying it.

See i have added the'\0' still dosent change

...

Hey narue
could you please just tell me exactly what to do where
because i have this assignment due in 30 mins. If i had more time I would have worked it out

I guess I missed the deadline. Well, now you have more time to do some reading up on how strings work in C. :)

Ah, 30 minutes - that explains all
http://cboard.cprogramming.com/c-programming/138916-copying-into-array-display-command-promt.html

http://www.catb.org/~esr/faqs/smart-questions.html#urgent

lol
so you sent me a link to read how to ask for help? So what bit of there was I unpolite or whatever .... and yeh i did read it and yeh i got it fixed

IF i was asking for help from scratch with no code id understand you guys being off handish a simple ... hey look at line so and so ... thats all i needed

anywho ... best not to ask help from other IT guys ... and i cant really point fingers at u guys only even the people i study with are the same

hey look at line so and so ... thats all i needed

No, that's clearly not all you needed, because this wasn't enough:

At a glance, you should terminate str before copying it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.