Reputation: 63
I'm struggling to compare the contents of a struct to a variable. There are 10 structs in array1 with the variables value and count. I need to go through every value variable until I find the one matching tempVal and then increment the corresponding count, then the search can end. If it's not found the function will return -1.
I have the following code that runs fine but doesn't work, I have a feeling it may be something wrong with the strcmp line but I'm not sure. Cheers for any input.
int valCheck(char *tempVal){
int j;
for(j=0;j<10;j++){
if(strcmp(array1[j].value, tempVal) == 0){
array1[j].count++; //increment its count
break;
}
}
}
Edited full:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
struct values
{
char value[64];
int count;
};
struct values array1[100];
//check if value exists in array
int valCheck(char *tempVal)
{
int j;
for(j=0;j<10;j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
//return j; // <== return index of found element
}
}
return -1;
}
int main()
{
FILE * input;
int i;
char tempVal[] ="hello";
input = fopen("random.txt","r");
for (i=0;i<=10;i++) //go through the 10 elements in text file
{
//scan each word into a temporary variable
// **** NOT COMPLETE, JUST USING TEMPWORD ASSIGNED FOR NOW
int checkedVal = valCheck(&tempVal);
//if wordCheck returns -1 add the word to the array
//otherwise do nothing as a duplicate has appeared
if(checkedVal == -1){
fscanf(input, "%s",array1[i].value);
}
printf("WORD %i: %s ",i,array1[i].value);
printf(" COUNT IS: %i", array1[i].count);
printf("\n");
}
fclose(input);
return 0;
}
Upvotes: 0
Views: 5187
Reputation: 66194
Assuming the following:
value
is a valid char *
or fixed-length char[n]
buffer.value
member of your structure is properly null-terminated.tempVal
is valid and properly null-terminated.strcmp()
compares case-sensitivelyMy crystal ball tells me that ultimately you need to have your function actually return something.
int valCheck(char *tempVal)
{
int j;
for(j=0;j<10;j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j; // <== return index of found element
}
}
return -1; // <== return -1 per your description of failure.
}
Note: Your compiler, equipped with decent warning-checks, would easily spot this lack-of-return code. Heed those warnings. Likewise, double-check everything in the bullet list at the top of this answer to make sure what you're going into this with is proper.
EDIT Updated to reflect sample dictionary build for OP.
The following is how I think this is likely to be called. Hope it helps you. I changed a few things:
I hope it helps.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct values
{
char value[64];
int count;
};
// global array.
struct values array1[256];
int n_words = 0;
//check if value exists in array
int valCheck(const char *tempVal)
{
int j;
for(j=0;j<10;j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j;
}
}
return -1;
}
int main(int argc, char *argv[])
{
FILE * input = NULL;
char tempVal[64];
int i=0;
if (argc < 2)
{
printf("Must specify a filename.\n");
return EXIT_FAILURE;
}
// open file
input = fopen(argv[1],"r");
if (!input)
{
perror("Failed to open file.");
return EXIT_FAILURE;
}
// read all strings from the file one at a time.
while (fscanf(input, "%64s", tempVal) == 1)
{
int i = valCheck(tempVal);
if (i == -1)
{
if (n_words < sizeof(array1)/sizeof(array1[0]))
{
strcpy(array1[n_words].value, tempVal);
array1[n_words].count = 1;
i = n_words++;
}
else
{ // error. no more space in dictionary
printf("No more space to add word: %s\n", tempVal);
}
}
}
fclose(input);
// summary report
for (i=0;i<n_words;++i)
printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count);
return EXIT_SUCCESS;
}
Input
hello my name is dave hello I am dave hi
Output
WORD 0: hello, COUNT IS: 2
WORD 1: my, COUNT IS: 1
WORD 2: name, COUNT IS: 1
WORD 3: is, COUNT IS: 1
WORD 4: dave, COUNT IS: 2
WORD 5: I, COUNT IS: 1
WORD 6: am, COUNT IS: 1
WORD 7: hi, COUNT IS: 1
Homework
After all of that, I leave you to determine why the following code also works, but uses no temporary buffer to do so (sort of, anyway).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
struct values
{
char value[64];
int count;
};
// global array.
#define MAX_WORDS 256
struct values array1[MAX_WORDS] = {{{0},0}};
int n_words = 0;
//check if value exists in array
int valCheck(const char *tempVal)
{
int j;
for(j=0; j< n_words; j++)
{
if(strcmp(array1[j].value, tempVal) == 0)
{
array1[j].count++; //increment its count
return j;
}
}
return -1;
}
int main(int argc, char *argv[])
{
FILE * input = NULL;
int i=0;
if (argc < 2)
{
printf("Must specify a filename.\n");
return EXIT_FAILURE;
}
// open file
input = fopen(argv[1],"r");
if (!input)
{
perror("Failed to open file.");
return EXIT_FAILURE;
}
// read all strings from the file one at a time.
while (n_words < MAX_WORDS &&
fscanf(input, "%64s", array1[n_words].value) == 1)
{
if (valCheck(array1[n_words].value) == -1)
{
array1[n_words].count = 1;
++n_words;
}
}
fclose(input);
// summary report
for (i=0;i<n_words;++i)
printf("WORD %i: %s, COUNT IS: %i\n", i, array1[i].value, array1[i].count);
return 0;
}
The output is the same as before.
Upvotes: 2