Thread: A simple program again!

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    19

    A simple program again!

    Code:
    int main() {
    
    
        char password[20];
        int i = 0, tru1, tru2, tru3;
    
    
        printf("Enter your password (Must have 1 uppercase, lowercase and and a digit!): \n");
        scanf(" %s", password);
    
    
        do {
                tru1 = 0; tru2 = 0; tru3 = 0;
            if (isupper(password[i])) {
                tru1 = 1;
            } else if (islower(password[i])) {
                tru2 = 1;
            } else if (isdigit(password[i])) {
                tru3 = 1;
            } else if (tru1 == 1 && tru2 == 1 && tru3 == 1) {
                printf("Congratulations you did it right your password is: %s", password);
                break;
            } else {
                printf("Something went wrong!");
                break;
            }
            password[i++];
        } while (i <= 20);
        return 0;
    }
    It keeps on printing something went wrong? What's wrong with my computer logic :O XD

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    You're setting the values of tru1/2/3 to zero each time the loop cycles so your good boy message will never be shown. Also, entering a passwords which exceeds 20 characters can lead to really bad things in your program.
    This parameter is reserved

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This is where learning about debuggers comes in handy.

    With a debugger, you would have been able to single step the code and watch variables changing line by line.
    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.

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Quote Originally Posted by Ktulu View Post
    You're setting the values of tru1/2/3 to zero each time the loop cycles so your good boy message will never be shown. Also, entering a passwords which exceeds 20 characters can lead to really bad things in your program.
    Oh yeah, now it works Lol. Why bad? I don't get it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by YaZao View Post
    Oh yeah, now it works Lol. Why bad? I don't get it.
    Get a large jug of water and pour it into a smaller jug of water.
    How do you decide when to stop pouring to avoid your feet getting wet?

    > char password[20];
    Now consider how scanf would know to stop at 20 characters.
    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.

  6. #6
    Registered User
    Join Date
    Jul 2015
    Posts
    19
    Quote Originally Posted by Salem View Post
    Get a large jug of water and pour it into a smaller jug of water.
    How do you decide when to stop pouring to avoid your feet getting wet?

    > char password[20];
    Now consider how scanf would know to stop at 20 characters.
    Ohh so I see, I should make it bigger then XDDD Ahahaa, oh yeah. I should also think of something like that when using scanf. Thanks! :-D

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ohh so I see, I should make it bigger then XDDD Ahahaa,
    No, you should consider how scanf() would know to stop before it overruns the bounds of the array.

    Never use any function that doesn't limit the number of characters it will retrieve into a C-string. You need to use correct the optional width specifier with scanf() to limit the number of characters it will retrieve. I suggest you find some documentation for this standard function for more information.
    Last edited by jimblumberg; 09-23-2015 at 08:10 AM.

  8. #8
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi all...

    Am I missing somethiing or have I been lead up the garden path.... thought the functions printf and scanf Both require the #include <stdio.h> header....? which is not shown in the code, so why would it work at all....?

    John

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by JohnGM View Post
    Hi all...

    Am I missing somethiing or have I been lead up the garden path.... thought the functions printf and scanf Both require the #include <stdio.h> header....? which is not shown in the code, so why would it work at all....?

    John
    The includes have almost certainly been left off for brevity.
    It happens a lot.

    But you are right, #include <stdio.h> would be needed.
    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. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Need help with simple, simple program.
    By LightsOut06 in forum C Programming
    Replies: 5
    Last Post: 09-01-2005, 08:31 PM

Tags for this Thread