Thread: Help with if statement comparing a Char

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    Help with if statement comparing a Char

    Code:
    if(publisher>=pub1loop && publisher<=pub2loop){
    				ret = 1;/*return 1 if the publisher sents falls between*/		
    			}
    i think this is where my problem is, because ret is never setting to 1

    example i send in "70002" <- publisher char

    and pub1loop equals "70000" and pub2loop equals "79999"

    this is my full function:
    Code:
    int registered(FILE* fp, int area, const char* publisher){
        	int pub1loopint, pub2loopint, ret=0;
        	char pub1loop[7], pub2loop[7], arealoop[7], areatostring[7];
        	sprintf(areatostring, "%i", area);
        	rewind(fp);
        	while(!feof(fp)) {
    		fscanf(fp, "%s %c %c", arealoop, pub1loop, pub2loop);
    		if(strcmp(areatostring,arealoop)==0){/*do this when the two areas are the same*/	
    			pub1loopint = atoi(pub1loop);
    			pub2loopint = atoi(pub2loop);	
    			if(publisher>=pub1loop && publisher<=pub2loop){
    				ret = 1;/*return 1 if the publisher sents falls between*/		
    			}
    		}
        	}
        	return ret;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fscanf(fp, "&#37;s %c %c", arealoop, pub1loop, pub2loop);
    Use %s, not %c to read into a string.

    And strings are compared with either strcmp() or strncmp(), like you did here:
    if(strcmp(areatostring,arealoop)==0){/*do this when the two areas are the same*/

    And it seems you could actually make arealoop an int. Then you could compare area directly with arealoop, without converting area first.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    Quote Originally Posted by swoopy View Post
    > fscanf(fp, "%s %c %c", arealoop, pub1loop, pub2loop);
    Use %s, not %c to read into a string.

    And strings are compared with either strcmp() or strncmp(), like you did here:
    if(strcmp(areatostring,arealoop)==0){/*do this when the two areas are the same*/

    And it seems you could actually make arealoop an int. Then you could compare area directly with arealoop, without converting area first.
    it was %s before but i changed it to see if that'll work

    and if i use strcmp how do i find if it between pub1loop and pub2loop

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Make publisher an int first!
    Like:
    intPublisher = atoi(publisher);

    Apart from that it seems correct.
    If the above doesn't work, printf() some variables, like pub1loopint, pub2loopint to see if you get the correct data

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Don't use atoi; use strtol.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >and if i use strcmp how do i find if it between pub1loop and pub2loop
    Here's what you have now:
    Code:
    			if(publisher>=pub1loop && publisher<=pub2loop){
    So try something like:
    Code:
    			if (strcmp(publisher, pub1loop) >= 0 && strcmp(publisher, pub2loop) <= 0 ) {
    And as suggested, if the value of publisher is 9 digits or less and won't contain leading zeros, you could use a long and forget about using a string comparison.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. Extra printed stmts...why?
    By mangoz in forum C Programming
    Replies: 4
    Last Post: 12-19-2001, 07:56 AM