Thread: Debugging password safe thing. Need help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Question Debugging password safe thing. Need help

    ok so im quite new to C and i am learning it atm, (cut me som slack). so im going thru the c for dummies book and came to the point of using scanf and printf and whas thinking to take the program one step futher. so i made it like so that if you answer correctly it will just to the next question but i had some minor bugs.
    And i know 2+2=4 kinda thing but it is just not that easy for me :P hehe
    Source code:
    Code:
    #include <stdio.h>
    int main()
    {
    char name[20];
    char color[20];
    printf("What is your name?");
    scanf("%s",name);
    if (strcmp(name,"david") == 0);
    else printf ("get away from the pc!");
    printf("What is your favorite color?");
    scanf("%s",color);
    if (strcmp(color,"blue") == 0);
    printf("%s’s favorite color is %s, oh i forgot... the password is david\n",name,color );
    return(0);
    }
    And now the bugs
    Code:
    john@ubuntu:/home/prog/C$ ./safe
    What is your name?idunno
    get away from the pc!What is your favorite color?youarenotstopping
    idunno’s favorite color is youarenotstopping, oh i forgot... the password is david
    john@ubuntu:/home/prog/C$
    The point is that if you answer wrong the program should end. now ive been thinking that is ther a way to make it show the text "get away from the computer" and then count down from 5 sec and close terminal ?

    Any answer is welcome thank you

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    else {
      printf("Get away from the pc!\nShutting down in five seconds.\n");
      delay(5);  //check your compiler whether it's delay(seconds), or sleep(seconds)
    }
    You'll need to include the right header file to use delay() or sleep(). Check your man pages.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by Adak View Post
    Code:
    else {
      printf("Get away from the pc!\nShutting down in five seconds.\n");
      delay(5);  //check your compiler whether it's delay(seconds), or sleep(seconds)
    }
    You'll need to include the right header file to use delay() or sleep(). Check your man pages.
    Thx so much

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nesiory View Post
    Code:
    #include <stdio.h>
    int main()
    {
    char name[20];
    char color[20];
    printf("What is your name?");
    scanf("%s",name);
    if (strcmp(name,"david") == 0);
    else printf ("get away from the pc!");
    printf("What is your favorite color?");
    scanf("%s",color);
    if (strcmp(color,"blue") == 0);
    printf("%s’s favorite color is %s, oh i forgot... the password is david\n",name,color );
    return(0);
    }
    See the text I colored red above...

    When you put a semi-colon after an if statement like that, it ends up doing nothing.
    Also the orphaned else is not going to work either.

    Try it like this...
    Code:
    if (strcmp(name,"david") != 0)  
      { puts("Get away from the pc!");
         _sleep(5);
        exit(0); }
    
    // rest of code goes here
    The only way to get to the rest of the code is to enter david. Everything else aborts the program.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Talking

    Quote Originally Posted by CommonTater View Post
    See the text I colored red above...

    When you put a semi-colon after an if statement like that, it ends up doing nothing.
    Also the orphaned else is not going to work either.

    Try it like this...
    Code:
    if (strcmp(name,"david") != 0)  
      { puts("Get away from the pc!");
         _sleep(5);
        exit(0); }
    
    // rest of code goes here
    The only way to get to the rest of the code is to enter david. Everything else aborts the program.
    Thank you so much help alot

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Thumbs up

    Ok so now it is finishd

    Code:
    #include <stdio.h>
    int main()
    {
    char name[20];
    char color[20];
    char key[20];
    printf("What is your name?");
    scanf("%s",name);
    if (strcmp(name,"david") != 0)  
    { puts("Get away from the pc!");
    sleep(5);
    return(0); }
    printf("What is your favorite color?");
    scanf("%s",color);
    if (strcmp(color,"blue") != 0)  
    { puts("Get away from the pc!");
    sleep(5);
    return(0); }
    printf("What is the master key?");
    scanf("%s",key);
    if (strcmp(key,"cboard") != 0)  
    { puts("Get away from the pc!");
    sleep(5);
    return(0); }
    printf("%s’s favorite color is %s, oh i forgot... the password is david\n",name,color );
    return(0);
    }
    Thank you so much for all the help

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Quote Originally Posted by Salem View Post
    Ok cleaned it up a bit

    Code:
     
      /***************************/
     /**Program made by Nesiory**/
    /***************************/
    #include <stdio.h>
    int main()
    {
    char name[20];
    char color[20];
    char key[20];
    
    printf("What is your name?");
    scanf("%s",name);
    if (strcmp(name,"david") != 0)   		/** Change (name,"ThisChange") to change name **/
    {
    puts("Get away from the pc!");
    sleep(5);
    return(0);
    }
    
    printf("What is your favorite color?");
    scanf("%s",color);
    if (strcmp(color,"blue") != 0)  		/** Change (color,"ThisChange") to change color **/
    {
    puts("Get away from the pc!");
    sleep(5);
    return(0);
    
    }
    printf("What is the master key?");
    scanf("%s",key);
    if (strcmp(key,"david") != 0)  			/** Change (Key,"ThisChange") to change master key **/
    {
    puts("Get away from the pc!");
    sleep(5);
    return(0);
    }
    
    printf("%s’s favorite color is %s, oh i forgot... the password is david\n",name,color );
    return(0);
    }

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    No, you did not. Didn't you read the link Salem gave you?

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    Yes i read the link. (bizzilion times now) and i think i still quite cant gett it :?
    But what about this and can someone point out what is wrong ?
    Thanks.

    Code:
       /***************************/
      /**Program made by Nesiory**/
     /****All Rigths Reserved****/
    /***************************/
    
    #include <stdio.h>
    int main()
    {
        char name[20];
        char color[20];
        char key[20];
    
    	/**Question ........../
        printf("What is your name?");
        scanf("%s",name);
        if (strcmp(name,"david") != 0)   		/** Change (name,"ThisChange") to change name **/
        {
          	puts("Get away from the pc!");
          	sleep(5);
    	return(0);
        }
    	
    	/**Question B**/
        printf("What is your favorite color?");
        scanf("%s",color);
        if (strcmp(color,"blue") != 0)  		/** Change (color,"ThisChange") to change color **/
        {
    	puts("Get away from the pc!");
    	sleep(5);
    	return(0);
        }
    
    	/**Question C**/
        printf("What is the master key?");
        scanf("%s",key);
        if (strcmp(key,"david") != 0)  		/** Change (Key,"ThisChange") to change master key **/
        {
    	puts("Get away from the pc!");
        	sleep(5);
        	return(0);
        }
    
    	/**Sulution**/				/** Change (Password is "ThisChange"\n",name,color) to change the final password **/
        printf("%s’s favorite color is %s, oh i forgot...the password is david\n",name,color );
        return(0);
    }

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You appear to have gotten the hang of moving things left-and-right. That is a good start, and you were consistent with it which is even better. Now you just need to fix the compile errors and you'll have something:
    Code:
    C:\Documents and Settings\Andrew\Desktop\temp.c:16:40: warning: "/*" within comment
    C:\Documents and Settings\Andrew\Desktop\temp.c: In function `int main()':
    C:\Documents and Settings\Andrew\Desktop\temp.c:19: error: `sleep' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:19: warning: unused variable 'sleep'
    C:\Documents and Settings\Andrew\Desktop\temp.c:26: error: `strcmp' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:29: error: `sleep' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:29: warning: unused variable 'sleep'
    C:\Documents and Settings\Andrew\Desktop\temp.c:26: warning: unused variable 'strcmp'
    C:\Documents and Settings\Andrew\Desktop\temp.c:36: error: `strcmp' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:39: error: `sleep' was not declared in this scope
    C:\Documents and Settings\Andrew\Desktop\temp.c:39: warning: unused variable 'sleep'
    C:\Documents and Settings\Andrew\Desktop\temp.c:36: warning: unused variable 'strcmp'

  12. #12
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    You need string.h to use strcmp().
    "All that we see or seem
    Is but a dream within a dream." - Poe

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    Question

    I could compile and run the program completely fine under Ubuntu 10.10
    No problems or errors here.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nesiory View Post
    I could compile and run the program completely fine under Ubuntu 10.10
    No problems or errors here.
    Different platforms and OSs will have differing requirements, of course. I would include string.h anyway. It may not make a difference with this tiny program but if you push through and get into some more extensive projects, portability may well become an important issue.

    I should also point out that indentation matters beyond mere cosmetics. If you ever have to come back 5 years later and re-work some code you wrote, the text layout, variable names and comments will be your best reminder of what you did "way back then". I've had this a few times in my life and on a couple of occasions I found I couldn't even read my own code... so, yeah, I got busy with correct formatting.

    In general function and variable names provide clues to what a function or block of code is doing in your overall program. The level of indentation provides important clues about "code within code" nesting to show you what's inside what... Comments should explain anything that's not immediately obvious... It's just good practice and it can save a lot of time and trouble down the road.

    Anyway... congrats on getting it to work...

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > temp.c:16:40: warning: "/*" within comment
    That's probably because the profanity filter had a go at 'A' followed by ** and munged that line of code.

    That removed the true end of comment, and made the rest of the program invalid.

    B** etc is not a problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PwdFilt
    By jgelowitz in forum C Programming
    Replies: 9
    Last Post: 12-18-2008, 01:41 PM
  2. Password Problem
    By peckitt99 in forum Windows Programming
    Replies: 6
    Last Post: 11-13-2007, 03:13 AM
  3. Password, color
    By leinad079 in forum C Programming
    Replies: 8
    Last Post: 03-16-2003, 05:10 PM
  4. Password prompt in unix w/o \b
    By rafe in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2002, 08:54 AM
  5. WINRAR password recovery
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-30-2002, 09:04 PM

Tags for this Thread