0

Password validation ( the problem in code coach )

I am facing a problem in the following code , i got all test cases correct except only one Can you please point out the mistake ??? Write a program that takes in a string as input and evaluates it as a valid password. The password is valid if it has at a minimum 2 numbers, 2 of the following special characters ('!', '@', '#', '

#x27;, '%', '&', '*'), and a length of at least 7 characters. If the password passes the check, output 'Strong', else output 'Wea #include <iostream> #include <string.h> using namespace std; int main() { char pass[20],ch[10]={'!', '@', '#', '
#x27;, '%', '&', '*'}; int t, num,chr; cin>>pass; for(int i=0;i<strlen(pass);i++){ if(strlen(pass)<7){ cout<<"Weak"; goto jump; } t = isdigit(pass[i]); if(t==1) ++num; for ( int j=0;j<strlen(ch);j++){ if(pass[i]==ch[j]) ++chr; } } if(chr<2){ cout<<"Weak"; goto jump; } if(num<2){ cout<<"Weak"; goto jump; } cout<<"Strong"; jump: return 0;

28th Sep 2021, 3:37 AM
Learn Sanj
Learn Sanj - avatar
3 Antworten
+ 3
You forgot to initialize num and chr variables before their uses. You wrote int num, chr when it should have being int num = 0, chr =0 So this code shall work fine: #include <iostream> #include <string.h> using namespace std; int main() { char pass[20],ch[10]={'!', '@', '#', '
#x27;, '%', '&', '*'}; int t, num = 0,chr = 0; cin>>pass; for(int i=0;i<strlen(pass);i++){ if(strlen(pass)<7){ cout<<"Weak"; goto jump; } t = isdigit(pass[i]); if(t==1) ++num; for ( int j=0;j<strlen(ch);j++){ if(pass[i]==ch[j]) ++chr; } } if(chr<2){ cout<<"Weak"; goto jump; } if(num<2){ cout<<"Weak"; goto jump; } cout<<"Strong"; jump: return 0; }
28th Sep 2021, 4:47 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Thanks bro!! It worked . But in c++ if a int variable is not assigned means the default value will be 0 right??
28th Sep 2021, 10:00 AM
Learn Sanj
Learn Sanj - avatar
+ 1
Learn Sanj , not in this case. If variable is global or static it will be zero initialized, but in this case variable is located in Main, so it's behaviour is unknown and compiler can't decide if it shall be zero initialized... (Static variables may only be initialized once) So another way to fix it is: static int t, num, chr;
28th Sep 2021, 10:35 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar