Thread: Need help please can't get working dynamic array at strcmp

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    21

    Need help please can't get working dynamic array at strcmp

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZEOFNAME 25
    typedef struct names
    {
    	char nameCheck[SIZEOFNAME];
    }Names;
    
    
    void countNames(int qty)
    {
    	FILE *text = fopen("names.txt", "r");
    	int i = 0;
    	Names *temp = (Names*)malloc(qty*sizeof(Names));
    	char str[SIZEOFNAME];
    	while (!feof(text))
    	{
    		fgets(str, SIZEOFNAME, text);
    		_flushall();
    		strcpy(temp[i].nameCheck, str);
    		i++;
    	}
    	int nameDuplicates=0;
    	int flag=0;
    	for (i = 0; i < qty-1; i++){  // string compare with next line
    		flag=strcmp(temp[i].nameCheck,temp[i+1].nameCheck);
    		if (flag)
    			nameDuplicates++;
    	}
    	printf("The total number of names in text is %d and the same names is %d\n", qty, nameDuplicates);
    	fclose(text);
    	free(temp);
    }
    int main()
    {
    	int ch;
    	int qtyNames = 0; // How many names
    	int wordCount = 0;
    	FILE *text = fopen("names.txt", "r");
    	/**************************************************/
    	while (!feof(text))  // Counting lines in text documents with names
    	{
    		ch = fgetc(text);
    		if (ch == '\n')
    		{
    			qtyNames++;
    		}
    	}
    	fclose(text);
    	/**************************************************/
    	countNames(qtyNames);
    	puts("End\n");
    }
    Hello, need help please with comparing names from text document
    example:
    Name1
    Name2
    Name3
    Name1

    Target to put names in dynamic array and compare if there is similar names in text document
    like Name1 is 2 times it will print there are 2 names are duplicated
    if someone can tell me what I'm doing wrong very appreciating your help thank you.
    Last edited by nuklon; 12-24-2016 at 06:40 AM.

  2. #2
    Registered User
    Join Date
    Apr 2016
    Posts
    21
    Found my self
    if (strcmp(temp[i].nameCheck,temp[i+1].nameCheck)==0)
    nameDuplicates++;

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Some notes:


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'strcmp and strcpy' Not Working Correctly
    By hufnagel in forum C Programming
    Replies: 6
    Last Post: 04-22-2013, 08:24 AM
  2. Replies: 10
    Last Post: 12-03-2011, 02:26 PM
  3. scan 2 d array for match using strcmp
    By elipse in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2011, 01:38 AM
  4. Working with dynamic memory in C++
    By IndioDoido in forum C++ Programming
    Replies: 8
    Last Post: 10-31-2007, 03:58 PM
  5. Dynamic array not working
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2003, 11:03 PM

Tags for this Thread