Theres something wrong with if statement compiling but not working. help me!!!!!

Discussion in 'C Programming' started by akash201, Oct 21, 2018.

  1. akash201

    akash201

    Joined:
    Oct 21, 2018
    Messages:
    2
    Likes Received:
    0
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include<string.h>
    #include <math.h>

    int main()
    {
    int i;
    char password[50];
    int passwordstrength=0;
    printf("Please enter a strong password which includes \n an uppercase, \n a lower case, \n a number and \n a special character\n");
    scanf("%d", &password);
    for(i=1;i<50;i++){
    if(isupper(password)){
    printf("password contains a uppercase");
    passwordstrength++;
    break;
    }

    }
    return 0;
     
    akash201, Oct 21, 2018
    #1
    1. Advertisements

  2. akash201

    akash201

    Joined:
    Oct 21, 2018
    Messages:
    2
    Likes Received:
    0
    the if statement prints irrespective of the input password
     
    akash201, Oct 21, 2018
    #2
    1. Advertisements

  3. akash201

    JasKinasis

    Joined:
    Jun 27, 2018
    Messages:
    11
    Likes Received:
    4
    Location:
    /dev/null
    For starters, in your scanf statement you are reading an integer (%d) when you should be reading a string (%s).
    That would be the biggest problem.
    So change your scanf line to:
    Code (Text):

    scanf("%49s", &password);
     
    In case you are wondering, the reason I'm specifying %49s is because your character array "password" has a size of 50 characters. But the last character must always be a NULL. So a character array with a size of 50 has space for 49 characters and a NULL terminator at the end.

    So by specifying %49s - we ensure that scanf will not try to read more characters than your array can hold. Otherwise we are going to introduce a stack smashing/buffer overflow vulnerability into your program.

    Also, in your for loop, you need to be looping from i=0 to i<50.
    In C, it is important to remember that array indexes start at 0.
    By starting at index 1, you are always going to be skipping the first character in the password.
    So you should start at index 0.

    Also, you REALLY should put your code into code tags when posting code here!
    From the italisization of the letters towards the end of your post, I can see that the line:
    Code (Text):

    if(isupper(password))
     
    Was actually:
    Code (Text):

    if(isupper(password[i]))
     
    But because you didn't use code tags the array indexing part of that statement ("[i]") got interpreted by thecodingforums.com post editor as the markup entity for italic text. Which is why that line of code looks wrong.
    Please put all code inside [code][/code] tags.
    It will allow your code to retain it's original indentation and things like [i] in your code will not end up being misinterpreted by the editor as markup for italic text!
     
    JasKinasis, Oct 26, 2018
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.