What is wrong with this code?

This is a discussion on What is wrong with this code? within the C Programming forums, part of the General Programming Boards category; The code just won't compile. Can anyone see the problem with it? Code: include <stdio.h> int main() { char password ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    What is wrong with this code?

    The code just won't compile. Can anyone see the problem with it?



    Code:
    include <stdio.h>
    
    int main()
    
    {
    
    char password = "123";
    char check;
    
    printf ("Please enter the password");
    scanf( "%s",&check );
    
      while ( check != password ) {
         printf ("Wrong password. Try again.\n");
         scanf("%s", &check);
      }
    
    printf ("Password Correct");
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    - first line is missing "#" in front of "include"
    - variable "password" is a char but your trying to assign it a char*
    - variable "check" is a char but your trying to treat it as a string with scanf (buffer overflow)
    - "check != password" isnt how you compare strings, look at the functions in "string.h"

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cornacum View Post
    The code just won't compile. Can anyone see the problem with it?



    Code:
    include <stdio.h>
    
    int main()
    
    {
    
    char password = "123";
    char check;
    
    printf ("Please enter the password");
    scanf( "%s",&check );
    
      while ( check != password ) {
         printf ("Wrong password. Try again.\n");
         scanf("%s", &check);
      }
    
    printf ("Password Correct");
    
    return 0;
    
    }
    I think you want your password to check against string password which is 123. So you have to declare check as a string. I think this is what you're trying to achieve.
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    
    {
    
    char password[]="123";
    char check[10];
    int x;
    printf ("Please enter the password");
    scanf("%s",check);
     while ( strcmp(password,check) ) {
         printf ("Wrong password. Try again.\n");
         scanf("%s",check);
      }
    printf ("Password Correct");
    return 0;
    }
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    possibly try this...

    Code:
    while ( strcmp(password,check)!=0 ) {
         printf ("Wrong password. Try again.\a\n");
         scanf("%s",check);
    when using string compare, if you want to see if the string are the same you see what the value equals. if it equals '0' then they are identical. so !=0 just means they are not(!) equal to 0 or not the same string. this is useful when you are comparing strings in different ways. the value that it equals will be different. not necessary for this program but good to know.

    the backslash a (\a) makes an audible beep. so when the wrong password is entered it will beep and prompt for the password again. again not necessary, but fun.
    Last edited by ominub; 05-03-2009 at 12:30 AM.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by ominub View Post
    possibly try this...

    Code:
    while ( strcmp(password,check)!=0 ) {
         printf ("Wrong password. Try again.\a\n");
         scanf("%s",check);
    when using string compare, if you want to see if the string are the same you see what the value equals. if it equals '0' then they are identical. so !=0 just means they are not(!) equal to 0 or not the same string. this is useful when you are comparing strings in different ways. the value that it equals will be different. not necessary for this program but good to know.
    In this program !=0 only adds to the typing. Coz the loop will automatically terminate if strcmp returns 0.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by BEN10 View Post
    In this program !=0 only adds to the typing. Coz the loop will automatically terminate if strcmp returns 0.
    That doesn't mean it's wrong to be explicit though. I'd rather type a few extra characters and be explicit about what I mean.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by cpjust
    I'd rather type a few extra characters and be explicit about what I mean.
    I agree, especially for strcmp where one might accidentally forget that 0 means "equal" rather than "it is false that the strings compare equal".
    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

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cpjust View Post
    That doesn't mean it's wrong to be explicit though. I'd rather type a few extra characters and be explicit about what I mean.
    I didn't say that it woud be wrong to explicitly do that. But here's a line from K&R for the code of strcpy from section 5.5
    First here's the code
    Code:
    void strcpy(char *s, char *t)
    {
    while(*s++=*t++)
    ;
    }
    and after that he has written
    Although this may seem cryptic at first sight, the notational convenience is considerable, and the idiom should be mastered, because you'll see it frequently in C programs.
    So by not doing !=0, I was only following him because it's a standard book.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    Quote Originally Posted by BEN10
    So by not doing !=0, I was only following him because it's a standard book.
    You are mixing up ideas. The classical strcpy() implementation relies on the fact that after the last iteration, the source pointer points to a null character, so the loop terminates after assigning the null character to the destination string since a null character is evaluated to false.

    The idea of strcmp(), on the other hand, is that the value returned can be compared with 0 to determine the comparison between the two strings. So, if we use a "<" comparison with 0, we are asking if the left hand side string is less than the right hand side string. If we use a "!=" comparison with 0, we are asking if the left hand side string is not equal to the right hand side string.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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