#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Question Newb firstprog.c


    I've written and compiled the first program I've literally created without my learnin book in front of me. I have seemingly one bug in it... or one and a half i would almost say. Mostly i want to know how i did for like a 1 month beginner and if you have more ideas for programs to write to help me learn.

    im sorry i dont know the coding for html to make this look neater but if you have comments/criticism on this, lemme know. thanks

    pass.c
    #include <stdio.h>
    #include <string.h>

    int main() {
    char str_1[7];
    char str_2[7];

    strcpy(str_1, "Death");

    printf("What Is The Password?\n");

    scanf("%s\n", str_2);
    printf("%s, contemplating.\n", str_2);

    if(*str_2 == *str_1) {
    printf("success\n");
    printf("Change Password\n");
    scanf("\%s\n", str_1);
    printf("Success");
    printf("Pass is now-->\t %s\n", str_1);
    }

    else {
    printf("failure.\n");
    printf("You lose.\n");

    }
    }


    And the results seemed to be screwed by the first password as youll notice if you compile or just can read it and know whats going to be wrong. Like i said im a newbie, so hit me with as much criticism as you can.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Originally Posted by Typecasted
    im sorry i dont know the coding for html to make this look neater but if you have comments/criticism on this, lemme know. thanks
    Wrap your code in [code] [/code] tags and it will preserve your indentation and spacing.


    Code:
    if(*str_2 == *str_1) {
    That if statement is only going to be comparing the first character of both strings. *str_2 and *str_1 is the same as doing str_2[0] and str_1[0]. One might be tempted to try and compare the strings using if (str_2 == str_1) but that also will not work. That would compare the pointer values of str_2 and str_1, not the contents of the strings.

    To compare the contents of two strings, you need to loop through both strings and compare letter by letter to see if they are the same. There is a standard function to do this called strcmp. This function returns 0 if the strings are equal, non-zero otherwise.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Location
    India
    Posts
    73
    Rep Power
    0
    Originally Posted by kicken
    Wrap your code in [code] [/code] tags and it will preserve your indentation and spacing.


    Code:
    if(*str_2 == *str_1) {
    That if statement is only going to be comparing the first character of both strings. *str_2 and *str_1 is the same as doing str_2[0] and str_1[0]. One might be tempted to try and compare the strings using if (str_2 == str_1) but that also will not work. That would compare the pointer values of str_2 and str_1, not the contents of the strings.

    To compare the contents of two strings, you need to loop through both strings and compare letter by letter to see if they are the same. There is a standard function to do this called strcmp. This function returns 0 if the strings are equal, non-zero otherwise.
    I am also agree with this one, well in fact when I was fresher I have such error ant the above way is the smartest way to recover that.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0
    Well thank you for your input. I did not know of strcmp, so that can be most useful.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,953
    Rep Power
    4033
    Originally Posted by Typecasted
    Well thank you. I did not know of strcmp, so that can be useful. I'm curious though, as to why for the most part it does work with my error. (I mean as long as you type the password, then something random, it still knows you got the password right).
    With your code, anything you type which begins with the letter 'D' will pass and the program will consider it valid. Anything that does not start with 'D' will fail.

    Ex, enter 'Doom' as the password and it will work.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0
    I got that after thinking about it for a sec. Thought i edited that off =p

IMN logo majestic logo threadwatch logo seochat tools logo