943,913 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2798
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 9th, 2009
0

Re: what is strcmp do? strcmp work?

Click to Expand / Collapse  Quote originally posted by ubi_ct83 ...
its "a1,b1,c1".
i use const_array as double array so that everytime i just query the row,not the column.
and append_test _data is just a string array.
do u meant with this declaration :
char append_test_data [MAX_LENGTH]="";
i use "" in this array.does it effect?
normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Sep 9th, 2009
0

Re: what is strcmp do? strcmp work?

i think something like this would work
  1. for(int n=0;n<=sizeof SzStrings/SzStrings[0];n++)
  2. {
  3. Putrand(append_test_data);
  4. if(strcmp(append_test_data, SzStrings[n]) == 0)
  5. {
  6. printf("Append data is =%s\n",append_test_data);
  7. printf("constraint is found=%s\n", SzStrings[n]);
  8.  
  9. getch();
  10. return 0;
  11.  
  12. }
  13. }
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Sep 9th, 2009
0

Re: what is strcmp do? strcmp work?

I like the above discussion. Really. I can really learn from all of you, my masters.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ronelpadz is offline Offline
2 posts
since Sep 2009
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

Click to Expand / Collapse  Quote originally posted by MrNoob ...
normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it
i repeat my prob,
i have an array with 5 row of data and each row has these data:
const_array[0][]=a1,b1,c3,
const_array[1][]=a2,b1,c3,
const_array[2][]=a3,b2,c2,
const_array[3][]=a2,b3,c1,
const_array[4][]=a3,b2,c1,

then i want each of them are compared to this :
char append_test_data[100]="";
for note,the data in this array is always randomly changed but only 1 data each time.so,this means,the const_array just compare to 1 data in append_test_data in a time.

example:
append_test_data =a2,b1,c3, =>the program cant compare them
but if append_test_data=a3,b2,c1, =>only program can compare them.
whats wrong with my program?
  1. if ((in_const = fopen(const_file, "r+")) == NULL)
  2. {
  3. fprintf(stderr, "Cannot open constraint file.\n");
  4. exit(0);
  5. }
  6. char const_array[100][100];
  7.  
  8. int m=0 ;
  9. while(!feof(in_const)& m <=100)
  10.  
  11. {
  12. fgets(const_array[m],100,in_const);
  13. m++;
  14. }
  15.  
  16. for(int n=0;n<=m;n++)
  17. {
  18. if(strcmp(const_array[n],append_test_data) == 0)
  19.  
  20. {
  21. printf("Append data is =%s\n",append_test_data);
  22. printf("constraint is found=%s\n",const_array[n]);
  23.  
  24. getch();
  25. exit(0);
  26. }
  27. }

actually,
i call the file and load to an array in the same function.maybe it effect sumthing.
Reputation Points: 7
Solved Threads: 2
Junior Poster
ubi_ct83 is offline Offline
123 posts
since Aug 2009
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

Hi...
I read your information as per that i have some good information on strcmp which i want to share with you....The following error appears: 'strcmp' : cannot convert parameter 1
from 'char' to 'const char *'. I've already tried using single
quotations. the header file
only contains the struct contents. The whole program is part of an
example found in my course work. Does strcmp only compare two sets of
strings or can it be used to determine the end of the string as well?



#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include "program2v.h"

using namespace std;





int main()
{


crecord *string1, *string2, t_str;

*string1 = t_str;

/// the error while( strcmp(*string1->customername, "\0"))
{
*string2 = *string1 + 1;
while( strcmp(*string2->customername, '\0'))
{
if(strcmp( *string1->customername, *string2->customername) > 0 )
{
t_str = *string1;
*string1 = *string2;
*string2 = t_str;

}
string2++;
}
string1++;
}


return 0;
}...
Thanks for sharing the post....
Reputation Points: 10
Solved Threads: 1
Newbie Poster
neilkevins is offline Offline
3 posts
since Sep 2009
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

i found that,the lenght of the strings are different.
strlen (const_array[m])=10,while
strlen (append_test_data)=9.
the funny is,the last data of const_array is 9.thats why its only compile the last data in that array because only the last data has the same lenght with append_test_data.

a1,b1,c3, =strlen (const_array[0])=10
a2,b1,c3, =strlen (const_array[1])=10
a3,b2,c2, =strlen (const_array[2])=10
a2,b3,c1, =strlen (const_array[3])=10
a3,b2,c1, =strlen (const_array[4])=10
a3,b1,c2, =strlen (const_array[5])=9

wow!!!!!

anyone knows how its happens?
Reputation Points: 7
Solved Threads: 2
Junior Poster
ubi_ct83 is offline Offline
123 posts
since Aug 2009
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

it is used to compare the string letter by letter..if they are same then it return the value zero otherwise the difference in the ascii values in the first letter of the two string which are different.
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

  1. a1,b1,c3,
  2. a2,b1,c3,
  3. a3,b2,c2,
  4. a2,b3,c1,
  5. a3,b2,c1,
Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void) {
  6.  
  7. FILE *fp = NULL;
  8. char const_array[100][100];
  9. char append_test_data[100]="a2,b3,c1,";
  10. int ctr= 0, i = 0;
  11. char *p = NULL;
  12.  
  13. // open constraints file
  14. if ((fp = fopen("constraint.txt", "r")) == NULL) {
  15. printf("Cannot open constraint file.\n");
  16. return EXIT_FAILURE;
  17. }
  18.  
  19. // read file and load each line of text into array
  20. // don't forget to overwrite the '\n' that fgets reads into the buffer
  21. while (fgets(const_array[ctr], 100, fp) != NULL) {
  22. if ((p = strchr(const_array[ctr], '\n'))) {
  23. *p = '\0';
  24. }
  25. ctr++;
  26. }
  27.  
  28. // check constraint against append data
  29. for (i = 0; i < ctr; i++) {
  30. if (strcmp(const_array[i], append_test_data) == 0) {
  31. printf("Append data is =%s\n", append_test_data);
  32. printf("constraint is found=%s\n", const_array[i]);
  33. }
  34. }
  35.  
  36. fclose(fp);
  37. return 0;
  38. }
The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Sep 10th, 2009
0

Re: what is strcmp do? strcmp work?

you can also use strncmp or use strchr to remove new line after each time fgets reads the string
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Sep 11th, 2009
0

Re: what is strcmp do? strcmp work?

Click to Expand / Collapse  Quote originally posted by yellowSnow ...
For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

  1. a1,b1,c3,
  2. a2,b1,c3,
  3. a3,b2,c2,
  4. a2,b3,c1,
  5. a3,b2,c1,
Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void) {
  6.  
  7. FILE *fp = NULL;
  8. char const_array[100][100];
  9. char append_test_data[100]="a2,b3,c1,";
  10. int ctr= 0, i = 0;
  11. char *p = NULL;
  12.  
  13. // open constraints file
  14. if ((fp = fopen("constraint.txt", "r")) == NULL) {
  15. printf("Cannot open constraint file.\n");
  16. return EXIT_FAILURE;
  17. }
  18.  
  19. // read file and load each line of text into array
  20. // don't forget to overwrite the '\n' that fgets reads into the buffer
  21. while (fgets(const_array[ctr], 100, fp) != NULL) {
  22. if ((p = strchr(const_array[ctr], '\n'))) {
  23. *p = '\0';
  24. }
  25. ctr++;
  26. }
  27.  
  28. // check constraint against append data
  29. for (i = 0; i < ctr; i++) {
  30. if (strcmp(const_array[i], append_test_data) == 0) {
  31. printf("Append data is =%s\n", append_test_data);
  32. printf("constraint is found=%s\n", const_array[i]);
  33. }
  34. }
  35.  
  36. fclose(fp);
  37. return 0;
  38. }
The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.
U r the man!!!!
thanks a lot master...i'll learn more after this..
thanks again
Reputation Points: 7
Solved Threads: 2
Junior Poster
ubi_ct83 is offline Offline
123 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: no one has yet to answer this question
Next Thread in C Forum Timeline: C basics - incompatible error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC