Thread: problems with strcmp!

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    problems with strcmp!

    Hello guys,
    this is another of my homeworks, a very simple program.
    the user have to select "mode1" or "mode2", and a file to use.

    if he selects mode1, the program will ask an int number, then create a file and write randomly "adenina" or "citosina" or "guanina" or "timina" and stop.

    if the user selects mode2, the program will read the file specified, count how many times there's wrote "adenina" "timina" "guanina" or "citosina" and printf the results.

    now, there's no error (and no warnings!) when i compile it, and if i choose mode1 the program does everything he has to.
    but if i choose mode2, it returns to me an error (like if i wrote something different) .. then i tried to force the program in mode 2, just to see if it works.. and surprise! the program won't do anything!

    here you go with the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    int mode1(char*);
    int mode2(char*);
    int contarighe(char*);
    void contastringhe(int,char*,int*);
    
    int main(int argc, char** argv){
    	int choose;
    	if(argc!=3){
    	printf("Errore: inserire modalità e nome del file. \n");
    	exit(1);
    	}
    	if(strcmp(argv[1],"mode1")==0){
    		choose=mode1(argv[2]);
    		}else if(strcmp(argv[1],"mode2")==0){
    			choose=mode2(argv[2]);
    			}else printf("Comando non valido, specificare mode1 o mode2.\n");
    	if(choose==0 || choose==1){
    		printf("Il programma è stato eseguito correttamente.");
    			}
    	return 0;
    }
    
    int mode1(char* filename){
            int n,i,temp;
    	FILE* file;
    	file=fopen(filename,"wb");
            if(file==NULL){
    		printf("Il file non può essere creato, o non può essere aperto.\n");
    		exit(2);
    		}
    	printf("Inserire numero di stringhe.\n");
    	scanf("%d",&n);
    	srand(time(NULL));
    	for(i=0;i<n;i++){
    		temp=rand()%4;
    		switch(temp){
    		case 0: 
    			fprintf(file,"ADENINA \n");
    			break;
    		case 1:
    			fprintf(file,"GUANINA \n");
    			break;
    		case 2:
    			fprintf(file,"TIMINA \n");
    			break;	
    		case 3:
    			fprintf(file,"CITOSINA \n");
    			break;
    		default:
    			printf("Valore inaspettato.\n");
    			return -1;
    		}
    	}
    	fclose(file);
    return 0;
    }
    
    int mode2(char* filename){
    	int n;
    	int cont[4];
    	FILE* file;
    	file=fopen(filename,"r");
    	if(file==NULL){
    		printf("Il file non può essere letto.\n");
    		exit(3);
    		}
    	n=contarighe(filename);
    	contastringhe(n,filename,&cont[0]);
    	printf("Nel file erano presenti: \n %d ADENINA \n %d GUANINA \n %d TIMINA \n %d CITOSINA \n", cont[0],cont[1],cont[2],cont[3]);
    return 1;
    }
    
    int contarighe(char* filename){
    	char buffer[100];	
    	int n;
    	FILE* file;
    	file=fopen(filename,"r");
    	while (fgets(buffer,100,file)!=NULL){
    		n++;
    		}
    	fclose(file);
    	return n;
    }
    
    void contastringhe(int n,char* filename,int* cont){
    	char nomi[4][8]= {"ADENINA", "GUANINA", "TIMINA", "CITOSINA"};
    	char temp [8];
    	int i,j;
    	FILE* file;
    	fopen(filename, "r");
    	for(i=0;i<n;i++){
    		fscanf(file,"%s",&temp);
    		for(j=0;j<4;j++){
    		          if (strcmp(nomi[j],temp)==0){
    				cont[j]++;
    				}
    			}
    		}
    	fclose(file);
    }
    Last edited by Darkobra; 07-14-2010 at 11:34 AM. Reason: now it is easier to read..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you make your indentation more consistent. You have some indentation, which is good, but the inconsistency makes it difficult to properly read some parts, e.g., the if statement chain in the main function.

    It is good also that you posted the code in code bbcode tags, but frankly you don't need so many blank lines. Use blank lines to separate parts of the code as you would separate paragraphs in an essay (e.g., it might separate a snippet of code that asks for user input and reads the input, and another snippet that further processes that input).

    You do not actually need the m1 and m2 variables as you can write:
    Code:
    if (strcmp(argv[1], "mde1") == 0) {
    etc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Darkobra View Post
    Hello guys,
    this is another of my homeworks, a very simple program.
    the user have to select "mode1" or "mode2", and a file to use.
    Code:
    	char m1[5]="mde1";
    
    	char m2[5]="mde2";
    
    	int choose;
    
    	if(argc!=3){
    
    	printf("Errore: inserire modalità e nome del file. \n");
    
    	exit(1);
    
    	}
    
    	if(strcmp(argv[1],m1)==0){
    
    		choose=mode1(argv[2]);
    
    		}else if(strcmp(argv[1],m2)==0){
    
    			choose=mode2(argv[2]);
    
    			}else printf("Comando non valido, specificare mode1 o mode2.\n");
    
    	if(choose==0 || choose==1){
    
    		printf("Il programma è stato eseguito correttamente.");
    
    			}
    
    	return 0;
    
    }
    Why should "mde1" match "mode1", and why should "mde2" match "mode2"?

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    yep, sorry for the blank lines, i just paste the code here, and they magically appears!
    well, whateaver..removed it!

    edited the code, now it works! thanks! I just didn't know that you can actually pass a string to strcmp.. my manual don't say it.. and in 3 examples, he always use a char variable .-.

    well, know, same as before:

    mode1 is right,
    but mode2 just doesn't give life signal. no errors, no printf, nothing at all. it just doesn't work.. can it be for the rand?

    @whiteflags, my mistake, i just tried to change mode into mde to see if it worked and then mistaken to tell ya :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Most Common problems in C
    By Bayint Naung in forum C Programming
    Replies: 18
    Last Post: 06-02-2010, 08:20 PM
  2. problems with strcmp
    By loopymoo26 in forum C Programming
    Replies: 6
    Last Post: 05-13-2009, 02:10 AM
  3. seg fault with strcmp
    By samps005 in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 04:32 PM
  4. strcmp, any borders?
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-15-2002, 02:48 AM
  5. strcmp problems
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-14-2002, 07:17 PM