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

Joined
Oct 21, 2018
Messages
2
Reaction score
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;
 
Joined
Jun 27, 2018
Messages
12
Reaction score
5
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:
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:
if(isupper(password))
Was actually:
Code:
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!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top