GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-May-2004, 11:26
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road

there are some errors but where??


hi every body i am sending a piece of code which is a part of my program. there is no problem in the compilation (except a warning C4013: 'calloc' undefined; assuming extern returning int ) but this part does not working properly actually this must have to print a letter matrix like:
d f r t
a t k l
v a n d
s e i c
this matrix is in the file and must be printed to the screen but it doesn't it prints something strange like :
d f r
a t
v
n d
s e i c

and one more problem when i write argv[1] for opening the file it does not open but as i put below when i write the files name to the code it works!!
if you can help me i will be very happy have a nice day!!!
CPP / C++ / C Code:
if(  (fptr = fopen( "deneme.txt","rb")) == NULL ) 
	{
		printf("the file could not be found!!\n");
		turn:	//label
		do
		{
			printf("please enter a valid file path:");
			printf("if you do not want to play the game just write EXIT");
			scanf("%s",path);
			if(!(strncmp(path,exit,4))){			//if the user does not want to play anymore 
				printf("you know! if you do not want to play go and find a more interesting game!");
				break;
			}
		}
		while((fptr=fopen( path ,"rb"))==NULL);			//if the file could not be found or the path does not correct ask the user to enter a valid path
	} else {
		fscanf(fptr, "%d", &n1); //height
		
		fscanf(fptr, "%d", &n2);	//width	
		matrix=(char**)calloc(n1,sizeof(char *));	//dynamic memory allocation for the matrix
		for(m=0;m<n1;m++){
			matrix[m]=(char*) calloc(n2,sizeof(char));
		}
		for(row=0;row<=n1;row++) {
			for(col=0;col<=n2;col++){
				do
				{
					c = fgetc(fptr);
				}
				while(c == ' ' || c=='\n');
				if( isalpha(c) == 1){
					printf("There is an unvalid character in the letter matrix:\n");
						goto turn;
				}
				matrix[row][col] = c;
				printf("%c ",c);
				counter++;
				if(counter%n1==0){
					printf("\n");
                                  }
                           }
                     }
Last edited by dsmith : 18-May-2004 at 16:38. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 18-May-2004, 12:12
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
can you plz include the main function along with all the include files that you are using?
__________________
spasms!!!
  #3  
Old 18-May-2004, 12:39
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
i have put the main part i think the mistake is in the part that i wrote and i have attached the file that i use in this program
CPP / C++ / C Code:
int main (int argc, char *argv[])
{
	FILE *fptr;
	char path[40];
	int start[1];
	int Start[1];
	char exit[5]="exit";
	char c;
	int n1,n2,number_of_words,number_of_rows=0,number_of_col=0,i,k,m,counter=0;
	char **words;   //words that will be searched in the matrix
	int row,col;
	char **matrix;
	if(  (fptr = fopen( "deneme.txt","rb")) == NULL ) 
	{
		printf("the file could not be found!!\n");
		turn:	//label
		do
		{
			printf("please enter a valid file path:");
			printf("if you do not want to play the game just write EXIT");
			scanf("%s",path);
			if(!(strncmp(path,exit,4))){			//if the user does not want to play anymore 
				printf("you know! if you do not want to play go and find a more interesting game!");
				break;
			}
		}
		while((fptr=fopen( path ,"rb"))==NULL);			//if the file could not be found or the path does not correct ask the user to enter a valid path
	} else {
		fscanf(fptr, "%d", &n1); //height
		
		fscanf(fptr, "%d", &n2);	//width	
		matrix=(char**)calloc(n1,sizeof(char *));	//dynamic memory allocation for the matrix
		for(m=0;m<n1;m++){
			matrix[m]=(char*) calloc(n2,sizeof(char));
		}
		for(row=0;row<=n1;row++) {
			for(col=0;col<=n2;col++){
				do
				{
					c = fgetc(fptr);
				}
				while(c == ' ' || c=='\n');
				if( isalpha(c) == 1){
					printf("There is an unvalid character in the letter matrix:\n");
						goto turn;
				}
				matrix[row][col] = c;
				printf("%c ",c);
				counter++;
				if(counter%n1==0){
					printf("\n");
				}
			}
		}
		number_of_words=fgetc(fptr);
		words=(char**)calloc(number_of_words,sizeof(char*));	//dynamic memory allocation for the words
		for(i=0;i<number_of_words;i++){
			words[i]=(char*)calloc(80,sizeof(char));			//dynamic memory allocation for all the worlds by one by one
		}
		
	for(i=0;i<number_of_words;i++){
		k=0;
		c=words[i][k];

	for(row=0;row<number_of_rows;row++){
		
		for(col=0;col<number_of_col;col++){
			
			if(c==matrix[row][col]){
				
				if(row==0 && col==0){
					
					if (eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col) ;

					}else if(southsearch(matrix,row+1,col,words,i,++k)){
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col) ;
					
						}else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col) ;
					
						}
											
				}else if(row==0&&col!=0) {
					if (eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row ;                               
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					
					}else if(westsearch(matrix,row,col-1,words,i,k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
						} else if(southsearch(matrix,row+1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
						
						}else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col) ;
						
						}else if(southwestsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col) ;
						}


				} else if (row!=0&&col==0) {

					if(eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row ;                               
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if(southsearch(matrix,row+1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					}else if(northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);

					} else if(southeastsearch(matrix,row+1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if(northeastsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",&words[i],&row,&col);

					}
				
				} else if (row==0&&col==number_of_col-1) {
					if(westsearch(matrix,row,col-1,words,i,++k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if (southsearch(matrix,row+1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col) ;
					}
				} else if (row==number_of_rows-1&&col==0) {
					if (eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row ;                               
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if(northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					}
				} else if (row==number_of_rows-1&&col!=0) {
					if(eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row ;                               
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if (westsearch(matrix,row,col-1,words,i,++k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if(northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					}
				} else if (row==number_of_rows-1&&col==number_of_col-1) {
					if (northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (westsearch(matrix,row,col-1,words,i,++k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					}
				} else if (row!=0&& col==number_of_col-1) {
					if(westsearch(matrix,row,col-1,words,i,++k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if (northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (southsearch(matrix,row+1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col) ;
					} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} 
				} else {
					if (eastsearch(matrix,row,col+1,words,i,++k)){
						start[0]=row ;                               
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if (westsearch(matrix,row,col-1,words,i,++k)) {
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col);
					} else if(northsearch(matrix,row-1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if(southsearch(matrix,row+1,col,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (southwestsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col) ;
					} else if (northwestsearch(matrix,row-1,col-1,words,i,++k)) {
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (northeastsearch(matrix,row-1,col+1,words,i,++k)){
							start[0]=row;                                
							Start[0]=col;
							printf("%c starts at position %d %d",words,&row,&col);
					} else if (southeastsearch(matrix,row+1,col+1,words,i,++k)){
						start[0]=row;                                
						Start[0]=col;
						printf("%c starts at position %d %d",words,&row,&col) ;
					}
				}
			}
		 }
	  }
	}			
  }				
  return 0;
}
Attached Files
File Type: txt proje.txt (508 Bytes, 9 views)
Last edited by dsmith : 18-May-2004 at 16:43. Reason: Please use [c] & [/c] for syntax highlighting
  #4  
Old 18-May-2004, 12:43
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
i meant post the header files that you are including for e.g. #include<stdlib.h>
__________________
spasms!!!
  #5  
Old 18-May-2004, 12:49
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
ok i am sorry

#include <stdio.h>
#include <ctype.h>
#include <string.h>
  #6  
Old 18-May-2004, 12:52
small_ticket small_ticket is offline
Junior Member
 
Join Date: May 2004
Posts: 45
small_ticket is on a distinguished road
i want to say one more thing now it gives another error at run time it says
"invalid allocation size 4294967292" what does this mean and what must i do??
and i have included stdlib.h and the warning that i mentioned has diappeared bu there is a new error as i said and it can not still read the file properly !!!
  #7  
Old 18-May-2004, 13:10
machinated machinated is offline
Regular Member
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
it probably has to do with the variable n1 you are reading it using fscanf, it might be reading a wrong value and then you are using n1 to allocate memory. sorry i can't be more specific or helpful as i'm not much of a c guy and have never used c memory allocation or file i/o functions before.


EDIT: it might also be this statement here: number_of_words=fgetc(fptr);
i assume it's supposed to get the filesize of fptr.
__________________
spasms!!!
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Error C2143: syntax error : missing ';' before 'type' small_ticket C Programming Language 6 15-May-2004 11:59
Can somebody look at this and point out any errors to me soulfly C Programming Language 7 31-Mar-2004 09:45
Rambus 128mb Pc800 memory errors momo Computer Hardware Forum 1 11-Mar-2004 04:22
PHP Compilation errors! Allowee MySQL / PHP Forum 17 21-May-2003 11:19
CGI Script Errors nniehoff Web Design Forum 2 13-Oct-2001 00:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.