Problems with code

This is a discussion on Problems with code within the C Programming forums, part of the General Programming Boards category; Hi, I'm pretty new to C and (once again) I have a problem with a 'piece' of code... Code: #include ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    53

    Problems with code

    Hi, I'm pretty new to C and (once again) I have a problem with a 'piece' of code...

    Code:
    #include <stdio.h>
    void main()
    {
    int x;
    printf("Type your password\n");
    scanf("%d", x);
    
    while (x!=12345){
        printf("Wrong, type your correct password\n");
        scanf("%d", x);
        if (x==12345)
        printf("Right password\n");
     }
    }
    The program asks the user for his password(12345), and if the user types the wrong password, the program keeps asking. The program runs on my compiler, but everything I type is wrong, what's wrong?
    Thanks

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    hmm, try giving the address of x, I'm pretty sure you need that, though it's been a while since I've used scanf

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    what's an address?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Use the address-of operator. e.g.: scanf("%d", &x);
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    B-e-a-utiful!!

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    main needs to return int, not void. When you've fixed that, also put a return 0; at the end of your main function.

    The other problem with your code is that your scanf will repeat if something is entered that's not a valid integer. This is because scanf will stop scanning when it hits the invalid character, and when you call it again, it will immediately hit it and stop, ad infinitum.

    See http://www.eskimo.com/~scs/C-faq/q12.19.html and associated FAQs.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    Code:
    #include <stdio.h>
    int main()
    {
    int x;
    printf("Type your password\n");
    scanf("%d", &x);
    
    while (x!=12345){
        printf("Wrong, type your correct password\n");
        scanf("%d", &x);
        if (x==12345)
        printf("Right password\n");
     }
    return 0;
    }
    Thanks for the tip, but if you type a non-integer (2.3, for example) it goes into an infinite loop, one of the problems that you told me about (though I made the changes you said?) so I'm not sure what happens now (lol)
    Thanks

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Alastor, before you post to a thread, please read all posts in it.

    The reason it goes into an infinite loop is in my post directly above your last post. Read the FAQ URL I posted.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    53
    I read a few parts of the FAQ, but I thought that if I did...
    main needs to return int, not void. When you've fixed that, also put a return 0; at the end of your main function.
    it would solve the problem, without seeing that it would still go into an infinite loop
    The other problem with your code is that your scanf will repeat if something is entered that's not a valid integer
    . I tried making X a double, but it didn't work... man, next time I read that scanf sucks I'll say: true that!

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    No, the FAQ I gave you explains directly why scanf behaves the way it does, and why you get an infinite loop. The infinite loop has nothing to do with your incorrect declaration of main.

    Also, scanf doesn't suck, it's just misunderstood. It stops reading when it hits a character that is unexpected for the format it has been given, this is documented and expected behaviour. If you don't like that behaviour, read the whole line using something like fgets and then use sscanf on the string, or similar as desired. If you'd bothered to read the FAQ I gave you in the first place, your problem would have been fixed more than a day ago.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  2. Problems with GetDiskFreeSpaceEx/my code...
    By scrappy in forum Windows Programming
    Replies: 1
    Last Post: 07-30-2003, 11:16 AM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM
  4. structs, array, code has problems, Im lost
    By rake in forum C++ Programming
    Replies: 4
    Last Post: 03-13-2002, 01:02 AM
  5. problems with output from code
    By simhap in forum C++ Programming
    Replies: 0
    Last Post: 10-08-2001, 12:43 PM

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