what's wrong with my solution? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

what's wrong with my solution?

l wrote this solution for Password Validation (Code Coach) and just 1 test failed (test 4)! what's wrong with this code?! #include <iostream> using namespace std; int main() { char c; int cs,cn,cc; while(cin>>c){ cc++; switch (c){ case '!': case '@': case '#': case '

#x27;: case '%': case '&': case '*': cs++; break ; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': cn++; break; } } if(cc>6 && cn>1 && cs>1) cout<<"Strong"; else cout<<"Weak"; return 0; }

14th Feb 2020, 1:43 PM
{ SorousH }
{ SorousH } - avatar
4 Answers
+ 3
Correctly initializing the counter variables to 0 solved the issue for me. The compiler doesn't do it for you automatically.
14th Feb 2020, 2:01 PM
Shadow
Shadow - avatar
+ 3
Nope. An uninitialized variable has whatever value has been stored previously in the memory cells it refers to. A simple way for default initialization is to use curly braces (uniform initialization), which works for all default-constructable data types (so basically everything), e.g. int a {}; char c {}; and so on.
14th Feb 2020, 2:23 PM
Shadow
Shadow - avatar
+ 2
Shadow Thank you so much 😀❤️ The problem solved. just one question, where's the problem with declaring a variable without initializing it and then using it?! isn't it 0 by default?
14th Feb 2020, 2:11 PM
{ SorousH }
{ SorousH } - avatar
+ 2
{ SorousH } In C++, local variables (especially of built-in type that aren't static) are not initialized to "zeros" for you, however, by default global and static variables are. So, where you place the variable makes the difference, even so it's advisable to always initialize your variables.
14th Feb 2020, 3:44 PM
G.T.T
G.T.T - avatar