Like Tree4Likes

i need help with a comparison between pointer and integer error

This is a discussion on i need help with a comparison between pointer and integer error within the C Programming forums, part of the General Programming Boards category; i want it to say "steven is cool" if i type in steven Code: #include <stdio.h> int main (int argc, ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    i need help with a comparison between pointer and integer error

    i want it to say "steven is cool" if i type in steven

    Code:
    #include <stdio.h>
    
    
    
    
    int main (int argc, const char * argv[]) {
    
    
    
    
    char steven;
    
    
    char bob[10];
    printf("\nname:");
    gets (bob);
    
    
          
    if (bob == steven) {  // i get it right here
        printf("steven is cool"); 
    }
    
    
    
    
        
        return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    bob is an array of 10 characters
    steven is a single character

    You can't compare a single character to a whole bunch of characters. They are different types of things. For starters, steven needs to be an array also. Next, assuming you want an actual string, you need to set it to contain something (for example, the string "steven"). Then you need to compare those strings with strcmp (assuming you don't want to loop through them both one letter at a time and compare them that way).

    Also, you should use something other than gets to read your input.
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    Cprogramming.com FAQ > Get a line of text from the user/keyboard (C)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You have one variable named steven which is empty, and a pointer that you try to compare (a char is an 8 bit integer). You can not compare strings with == in C, use something like strcmp().

    Code:
    char *steven = "steven";
    
    if( strcmp(bob, steven) == 0 ) {
         // they are equal
    }

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    thank you for your help

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    That should be:

    Code:
    const char *steven = "steven";
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    sure

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    more help needed

    Code:
    #include <stdio.h>
    
    
    
    
    int main (int argc, const char * argv[]) {
    
    
    char *david = "david";
    char *steven = "steven" ;
    char *daniel = "daniel";
        
    char bob[10];
    printf("\nname:");
    gets (bob);
    
    
          
    if ( strcmp(bob, steven) == 0) {  
        printf("steven is cool"); 
        
        {
          if ( strcmp(bob, david) == 0) {  
            printf("david is not"); 
              {
             if ( strcmp(bob, daniel) == 0) {  
                printf("daniel mabye"); 
                
                
            }
    
    
    
    
        
        return 0;
    }   /....i get  4  expected declaration or statement at end of input

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What do you need help with? You should declare *david and company with const char as suggested by King Mir. Those are string literals and should not be modified. Also don't use gets.

    BTW, you need to include string.h to use strcmp() should have mentioned that I guess.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    Some of your '{' should be '}'.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    thank you agian

    I just started programming a week ago I'm really I'm glad I found this website and thank you for you help......instead of the ending the program how would I get back to gets I don't care for loops I just wanna try and instead of gets what should I use for this program




    Code:
    #include <stdio.h>
    
    
    
    
    int main (int argc, const char * argv[]) {
    
    
    const char *david = "david",*steven = "steven", *daniel = "daniel";
    
    
    
    
    	
    char bob[10];
    printf("\nname:");
    gets (bob);
    
    
      	
    if ( strcmp(bob, steven) == 0) {  
    	printf("steven is cool"); 
    	
    }
    	  if ( strcmp(bob, david) == 0) {  
    		printf("david is not"); 
    	  }
    		 if ( strcmp(bob, daniel) == 0) {  
    			printf("daniel mabye"); 
    			
    			
            }
    
    
    
    
    	
    	return 0;
    }

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    fgets() but quzah provided a link that explained that. What you would do to replace gets in your code with fgets:

    Code:
    fgets(bob, 10, stdin);
    The middle argument will prevent bob from over flowing, the last argument is a file pointer, in this case stdin but you can also use fgets with files.
    Last edited by Subsonics; 10-03-2011 at 08:50 PM.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Instead of gets use fgets:
    Code:
    fgets (bob, sizeof(bob), stdio);
    And to repeat things multiple times, use a loop. That's what they're for. You're not qualified to decide what parts of the language you should and shouldn't use.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Quote Originally Posted by steevethebeeve1
    instead of gets what should I use for this program
    You could replace this:
    Code:
    char bob[10];
    printf("\nname:");
    gets (bob);
    with:
    Code:
    char bob[10];
    printf("\nname:");
    if (fgets(bob, sizeof(bob), stdin)) {
        /* If the newline was read, discard it */
        char *p = strchr(bob, '\n');
        if (p) {
            *p = '\0';
        }
    } else {
        /* Deal with the input error */
        /* ... */
    }
    Where the "..." could say, print an error message then end the program or something like that. Note that I used sizeof(bob) here because bob is an array of 10 characters; if you move this snippet to a function for which bob is a pointer instead, then you should replace sizeof(bob) with the variable holding the size of the array.
    King Mir, iMalc, Salem and 1 others like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by laserlight View Post
    ...
    Code:
    char bob[10];
    printf("\nname:");
    if (fgets(bob, sizeof(bob), stdin)) {
        /* If the newline was read, discard it */
        char *p = strchr(bob, '\n');
        if (p) {
            *p = '\0';
        }
    } else {
        /* Deal with the input error */
        /* ... */
    }
    Shorter with the same safety should be:

    Code:
    char bob[10];
    scanf("%9[^\n]",bob);while(getchar()!='\n');

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    Shorter with the same safety should be:
    Not even close...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparison between pointer and integer error
    By jb1989 in forum C Programming
    Replies: 2
    Last Post: 12-13-2010, 08:01 PM
  2. Comparison between pointer & integer
    By m88g88 in forum C Programming
    Replies: 5
    Last Post: 02-16-2010, 05:25 PM
  3. comparison pointer and integer
    By plodos in forum C Programming
    Replies: 19
    Last Post: 01-09-2009, 08:51 AM
  4. comparison between pointer and integer
    By R.Stiltskin in forum C Programming
    Replies: 13
    Last Post: 03-24-2007, 03:33 PM
  5. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21