0

I'm using dev c++ . This is silly program which ask the correct key to show a text. This program working perfectly with type "int" (just numbers):

    #include <stdio.h>
    #include<conio.h>

    main()
    {
        int key;

        printf("This program show a mensage if you get the correct key \n");
        printf("Write the key:\n");
        scanf("%i",&key);
            if(key==1234)
            {
                printf("Welcome to stackoverflow \n");
            }
            else
            {
                printf("You keep going slept \n");
            }
            getch();    
         return 0;

    }

But How can I replace for strings for example: sesame, house and so on.

I've tried by matrix:

char key[];

However i get Error.

Sincerily NIN.

Update:

I could get a new program :

#include <stdio.h>
#include<conio.h>

main()
{
    char key[7]; /*replace int for char*/

    printf("This program show a mensage if you get the correct key \n");
    printf("Write the key:\n");
    scanf("%c",&key);
        if(key=="sesame") /*numbers for string*/
        {
            printf("Welcome to stackoverflow \n");
        }
        else
        {
            printf("You keep going slept \n");
        }
        getch();    
     return 0;

}

However even though I fix the correct key ("sesame") I just get "You keep going slept"

NIN
  • 197
  • 13

3 Answers3

4

You cannot compare value of strings using == operator

      if(key=="sesame") // This compares pointers  

You need

      if(strcmp(key,"sesame") == 0)

See: http://www.tutorialspoint.com/c_standard_library/c_function_strcmp.htm

Also

  scanf("%c",&key);

will not work. %c gets only 1 character. You need %s for string.

int ret =  scanf("%6s", key); // or scanf("%6s", &key[0]); 
if (ret < 0)
     {
     // The function returns the number of input items successfully matched and assigned,
     // which can be fewer than provided for, or even zero in the event of an early matching failure.
     // The value EOF is returned if the end of input is reached before either the 
     // first successful conversion or a matching failure occurs. EOF is also returned 
     // if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error.
     // see: http://linux.die.net/man/3/scanf
     }

Note that you need just key or &key[0] as a pointer to the key buffer.

user2736738
  • 30,591
  • 5
  • 42
  • 56
sg7
  • 6,108
  • 2
  • 32
  • 40
  • 1
    Perhaps checking the return value of `scanf` would be wise along with using `scanf("'%6s", ....` to ensure no buffer overruns – Ed Heal Jun 11 '16 at 04:17
  • @EdHeal I agree! Protecting the buffer from overrun is important! I have improved the code. – sg7 Jun 11 '16 at 04:27
  • Your comment (`if successfull ....`) is incorrect - see [scanf](http://linux.die.net/man/3/scanf) – Ed Heal Jun 11 '16 at 04:28
  • @EdHeal I based my comment on this link: http://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm http://www.cplusplus.com/reference/cstdio/scanf/?kw=scanf has more description – sg7 Jun 11 '16 at 04:39
  • That is incorrect. See the manual page or try it yourself. PS: Scanf does not write - it reads – Ed Heal Jun 11 '16 at 04:42
  • @EdHeal OK! I understand. It reads from input. I was interpreting statement "characters written" as characters written to the buffer. I am improving the answer according to your suggestion. – sg7 Jun 11 '16 at 04:47
  • It is not the total number of characters. Please re-read the manual page – Ed Heal Jun 11 '16 at 04:49
  • @EdHeal Thank you for your comment! I have quoted the information from the [scanf](http://linux.die.net/man/3/scanf) link to improve the answer. – sg7 Jun 11 '16 at 05:05
1

Problem 1:

scanf("%c",&key);

is not right. It will read just one character. You need to use

scanf("%6s", key);

Problem 2:

    if(key=="sesame") 

is not the proper way to compare two strings. It will compare two pointers and will evaluate to false. You need to use:

    if( strcmp(key, "sesame") == 0 )
R Sahu
  • 204,454
  • 14
  • 159
  • 270
-1
#include <stdio.h>

int main()
{
    char *a = "sesame";

    printf("this program show a message if you get the correct key \n");

    printf("write the correct key:\n");

    //  scanf("%c",&key);

    if(a == "sesame")
    {   
        printf("welcome to stack overflow \n"); 
    }
    else
    {
        printf("you keep going to sleep \n");
    }
    return 0;
}
sg7
  • 6,108
  • 2
  • 32
  • 40
Deekshith
  • 13
  • 3