Thread: Issue with Strings

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Issue with Strings

    Hi There

    Hope someone can help. i am just getting started understanding chars and strings. i can get this code to compile and run but it fails to see when i have entered correct term i.e.

    I want you to guess which day of the month im thinking of!
    warning: this program uses gets(), which is unsafe.
    Please type in your month guess?march


    Please type in your month guess?april


    Please type in your month guess?feburary


    Please type in your month guess?


    here is the code, can anyone assist to help me figure out where im going wrong?

    Code:
     
    /* Program asking user to guess the correct month */
    #include <stdio.h>
    #include <string.h>
    
    
    main()
    {
    char guessdate[10]; /* Set char for month guesses */
    char *feburary = "feburary"; /* Set name of month for correct guess */
        
        
        
    printf("I want you to guess which day of the month im thinking of!");
        
        do {
    printf("\nPlease type in your month guess?");
            gets(guessdate);
            
        } while (guessdate != feburary); 
        
        if (guessdate == feburary)
    printf("\nCorrect!");
        
        
    return0;
        
    }
    im using x code on a macbook pro

  2. #2
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    In order to compare two strings, you need to use the "strcmp" function : strcmp - C++ Reference.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Brilliant thank you!! ill go check that out

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    32

    Smile

    QUOTE=breakboye;1072473]Hi There

    Hope someone can help. i am just getting started understanding chars and strings. i can get this code to compile and run but it fails to see when i have entered correct term i.e.

    I want you to guess which day of the month im thinking of!
    warning: this program uses gets(), which is unsafe.
    Please type in your month guess?march


    Please type in your month guess?april


    Please type in your month guess?feburary


    Please type in your month guess?


    here is the code, can anyone assist to help me figure out where im going wrong?

    Code:
     
    /* Program asking user to guess the correct month */
    #include <stdio.h>
    #include <string.h>
    
    
    main()
    {
    char guessdate[10]; /* Set char for month guesses */
    char *feburary = "feburary"; /* Set name of month for correct guess */
        
        
        
    printf("I want you to guess which day of the month im thinking of!");
        
        do {
    printf("\nPlease type in your month guess?");
            gets(guessdate);
            
        } while (guessdate != feburary); 
        
        if (guessdate == feburary)
    printf("\nCorrect!");
        
        
    return0;
        
    }
    im using x code on a macbook pro[/QUOTE]

    When you use if condition for comparison of two strings, it compares addresses rather than strings.
    Use strcmp( ) function to compare strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with cin and strings
    By putzboy20 in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2010, 08:38 PM
  2. Reading Lines from File Issue (integers and character strings)
    By kbfirebreather in forum C Programming
    Replies: 4
    Last Post: 10-17-2009, 02:02 PM
  3. "sizeof" character strings - output issue
    By bunko in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 06:10 PM
  4. Issue with strings
    By walden in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2003, 06:58 PM
  5. made some progress on strings.. still one issue
    By ss3x in forum C++ Programming
    Replies: 0
    Last Post: 04-21-2002, 10:37 PM