Thread: Guessing the Password

  1. #1
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9

    Guessing the Password

    Hey guys,
    I am new to the forums and pretty average at c programming at the moment, but there is this one program that i just cant get to run and it is annoying the hell out of me. please help, any advice is greatly appreciated


    Basically i am trying to get it to ask you to guess password and if you get it wrong it says try again, and if you get it right the program ends. I would like to add 3 choices to it as i learn more. i know this is the simplest program but I am determined to get it working, thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int pass;
      int guess;
    
      pass = 123;
      printf("Try and Guess My Password\n");
      printf("Password: %d ", guess);
      scanf("%d", guess);
    
      if (guess != pass) {
        printf("incorrect try again");
      } else {
        printf("You guessed it");
      }
      return 0;
    }
    Last edited by Salem; 05-28-2012 at 07:06 AM. Reason: removed pointless colour from code

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    I suggest you read up on scanf. You should be passing a pointer to guess, not guess itself.

    I also STRONGLY suggest that you always compile with all warnings enabled. That would have picked up this and lots of other problems for you automatically. I assume you didn't notice you'd printed guess before you'd inputted it?
    Last edited by ledow; 05-28-2012 at 06:50 AM.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9
    Thanks for the advice ledow, i am actually busy reading "C For Dummies" and the scanf examples in the book aren't exactly what i need and i cant really interpret the examples into what i am looking for. Here is the updated version of the source code. The error that I keep getting is " else without a previous if ". So it is not reading the if and else statement.
    Thanks in advance for the help and tips.

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    int main()
    {
        int pw = 246;
        int guess1;
    
    
        printf("Guess The Password\n");
        printf("Try 1: ");
        
        if(guess1 == pw);
        {
            printf("You guessed it");
        }
         else
        {
            printf("You got it wrong");
        }
        return (0);
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    pretty average at c programming
    No offense, but if scanf eludes you, you've not yet reached "average".

    Code:
    if(guess1 == pw);
    The ; at the end of that is your problem. And the fact you never get anything from the user and therefore guess1 can contain any value at all.

    FAQ > How do I get a number from the user (C) - Cprogramming.com

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well ofcouse it not going to work. You haven't read any value of the user for the variable guess have you? meaning the guess1 has a garbage value!

    >I suggest you read up on scanf. You should be passing a pointer to guess, not guess itself.
    What is means is, that scanf need the address of a variable to store the data. What you had was just the variable name. Which dosn't provide the address of a variable. Something like this

    Code:
    scanf("%d", &guess);
    This provides the scanf with the address of the variable guess1 which allows it store the user entered data in the memory.

    Just do this modification in your orginal code and you should have some progress.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    scanf can and will run amok if you let it. What happens if your user enters anything other than a digit? Once you've got your loop ironed out, try entering characters using scanf to see the little gremlins take control, and then mill over this. As for the wrong password, you'll want a loop. do-while is handy.

    Code:
    do {
    
       /* prompt for and read input */
       /* output message if wrong (if desired) */
    
    } while(test condition is false);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User Program_Me's Avatar
    Join Date
    May 2012
    Posts
    9
    Thanks for the help guys, i got the code to work and the if else statement is goin as predicted. I am sorry for all the errors in the code, i have only just started on this forum and still getting the hang of it.

  8. #8
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    In the future, use something like this website: scanf - C++ Reference to look up functions and see how they operate, what they should be given and what they should return (and, thus, what you should be CHECKING for them returning every time you use them.

    Learning programming from one book is a horrendous waste of time and effort, especially if they don't have some equivalent function listing within them. Find yourself some google-able online resources so you can look up how things work and not have to treat entire swathes of the C standard library like some kind of black box with no documentation.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  9. #9
    Registered User MyBlackSon's Avatar
    Join Date
    Dec 2011
    Location
    NSW, Australia
    Posts
    2
    Even though I am still sort of new to the C language, I am still aware of scanf(), I suggest not using it. The error has occurred at your scanf() statement, you need to include an ampersand (&) to store an integer in memory *from what I know*. So the statement will now be scanf("%d", &guess);

    Also for that bit how you wanted a limited amount of guesses, you would probably want to have a loop running with it set to loop through 3 times of the user trying to guess the password, if the condition is met then end the loop and if the condition is not met and the loop count is still less than 3 just increment the loop count.

    Hope this helped you
    Last edited by MyBlackSon; 05-30-2012 at 03:16 AM.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by MyBlackSon View Post
    I am still aware of scanf(), I suggest not using it.
    What do you suggest the OP use instead?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #11
    Registered User MyBlackSon's Avatar
    Join Date
    Dec 2011
    Location
    NSW, Australia
    Posts
    2
    I have read that a large majority of people prefer using an alternative such as gets(), fgets() or getchar(). For any wanted external help, I found this a helpful webpage (Question 12.20)


  12. #12
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You do realise that you just advocated recreating a scanf equivalent to a user who doesn't understand that all they needed was an ampersand (&) and that didn't know what it was or did?

    I'm not sure the advocation of avoiding scanf is entirely sensible anyway. About the only real problem with it (the examples linked are pretty minor and avoidable) is the "%s" format specifier being slightly insecure, which is easily avoided (don't use scanf with a %s for untrusted input). I think "recreating the wheel" of remaking scanf just because of that one entirely avoidable problem is a bit of overkill, where scanf's primary usage nowadays is in programming tutorials to receive input from the command line (which is a pretty unusual use scenario - I don't think there's much outside of ancient, well-catered-for uses of such stdin input nowadays) for which it does a pretty good job.

    scanf is fine here, and unlikely to make it into any "modern" program at all, and asking beginner users who have trouble using it to recreate an equivalent with some even more underpowered tools is counter-productive. Personally, I think the OP should ignore the suggestion and just fix their original program.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by MyBlackSon View Post
    I have read that a large majority of people prefer using an alternative such as gets(), fgets() or getchar(). For any wanted external help, I found this a helpful webpage (Question 12.20)

    This whole thing is way off base. Someone asks what you would use to get numbers instead, and you list functions that produce results... in text. In this case the link is not unwanted external help but required reading because you didn't mention what you would actually do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-07-2009, 10:35 AM
  2. Guessing program Hm. Help
    By snugy in forum C Programming
    Replies: 3
    Last Post: 11-17-2008, 11:09 AM
  3. Number Guessing
    By blacknapalm in forum C Programming
    Replies: 2
    Last Post: 10-01-2008, 01:48 AM
  4. Guessing Program
    By webren in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 07:24 PM
  5. guessing a number
    By Huma Ahmad in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2003, 10:09 AM