Thread: Problem with comparing strings!

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    17

    Problem with comparing strings!

    I have a problem with comparing strings.
    What i need is to write a program that reads a file input with numbers and strings, and put the numbers in one array x[] and the characters in another one y[] (characters with only one letter), and then compare the characters. But the comparing process does not work, and i do not know why. I am realy stuck in here.
    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 100
    #define N "N"
    
    void main()
    {
    unsigned int max, min;
    int x[MAX];
    char y[MAX], z[MAX], buf[3];
    FILE *file;
    int i, j;
    
    file = fopen("SPRATOVI.IN", "rt");
    
    fscanf(file, "%d", &j);
    max = 0;
    min = 0;
    for(i = 0;i < j;i++)
    {
    fscanf(file, "%d %c%c", &x[i], &y[i], &z[i]);
    printf("%d %c\n", x[i], y[i]);
    }
    for(i = 0;i < j;i++) 
    {
    if(strcmp(y[i], N) == 0)
    {
    if(x[i] > max)
    {
    max = x[i];
    }
    }
    else {
    if(x[i] < min)
    {
    min = x[i];
    }
    }
    
    }
    return;
    }
    The first for loop with the printf() is fine - i get the numbers and chars on screen.
    There is a problem with the second one, i.e. the strcmp() function, and i get just segmentation fault error. I have tried different methods for doing this, but no success.

    Please somebody help,
    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Pick one of these, and make sure your posts' adhere to your chosen style.
    http://en.wikipedia.org/wiki/Indent_style

    "None" isn't an option.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    I don't quite get the format of your programs I/O handling, tell me if I've got it right here. The first line in the .in file is the number of proceeding lines. An integer and two chars situate themselves on a single line, awaiting their respective entry into an array. (Side note: Why the second char? Though you put it in an array, you never bring attention to it ever again). If the char value is N, then the corresponding integer value is compared with the present min and max values. Respective replacements occur.

    If this is correct, then you do not need to use strcmp - you can directly compare two char values. See the program below.

    Code:
    #include <stdio.h>
    
    #define MAX 100
    #define N 'N'
    
    int main()
    {	
    	FILE 	*f;
    	int	x = 0, j, max, min, integers[MAX];
    	char	characters[MAX];
    	
    	/* Try opening the file */
    	if ((f = fopen("derp.in", "r")) == NULL)
    	{
    		printf("Sorry, could not open that file\n");
    		exit(1);
    	}
    	
    	/* Get first number (which I assume is the number of following lines) */
    	fscanf(f, "%d", &j);
    
    	/* get contents of the file */
    	for (x = 0; x < j; x++) 
    		fscanf(f, "%d %c", &integers[x], &characters[x]);
    
    	/* Set the min/max to be the first element in the array */
    
    	max = integers[0];
    	min = integers[0];
    
    	/* compare the characters */
    	for (x = 0; x < MAX; x++)
    	{
    		if (characters[x] == N)
    		{
    			if (integers[x] < min)
    				min = integers[x];
    			else if (integers[x] > max)
    				max = integers[x];
    		}	
    	}
    
    	printf("Max value: %d\n", max);
    	printf("Min value: %d\n", min);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem sorting an array of strings
    By Leojeen in forum C Programming
    Replies: 7
    Last Post: 05-07-2008, 09:02 PM
  2. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  3. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 11:41 PM
  4. Newbie having problem with strings
    By sunzoner in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2002, 08:34 PM
  5. Comparing strings...
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-18-2002, 05:42 AM