I'm trying to get "while (r !=4)" to properly operate in a "do-while" statement.
I know next to nothing about the "do" statement which is discussed later in my textbook, but our teacher told us to use it according to his example. If I use each function listed solo within the "do" statement, it will operate as intended. So I know my later functions are correct, and the problem lies within the main body.
The problem is no matter my input the "while" loop passes and the "do" statement does not give me the proper errors when intended. However if I eliminate the math for "r", it asks me to retype the password for every character entered.
Since there are no warnings or errors, I am more than a little lost and any help or advice would be greatly appreciated.
int main ()
{
char password [8];
int a, b, c, d, r;
explanation ();
do
{
printf("Please enter your password:");
scanf_s("%s",password);
printf("\n");
b = no_symbols(password);
if(b==0)
printf("You can only have alpha numeric characters in your password.\n");
c = at_least_one_digit (password);
if (c==0)
printf ("There must be at least one numeric character in your password.\n");
d = at_least_one_letter(password);
if (d==0)
printf ("Your password must contain at least one letter.\n");
a = length_check (password);
if (a==0)
printf ("You must have exactally 8 alpha numeric characters!\n");
r= a + b + c +d;
}
while(r !=4);
printf("Legal password!\n");
return 0;
}
Yes, I had originally written it where it came after line 33, however it gives me a C2061 identifer "r" syntax error when I go out of the {}. That's a good idea though. I'll make sure to have it print the values of a-r to make sure they're correct.
The "r" section is functioning as intended from testing it as per your suggestion. The problems seem to be pointing twoards the setup of the functions within my "do" statement. It seems reguardless of input the the values of a,b,c,d are turned to 1 "true". When I had tested each "if" statement on their own they functioned properly within the program, so I'm guessing my setup of "if" statements need to be adjusted to something like "if-else" or I need a seperate "do" statement for each individual "if" statement.
I'm using MS Visual C++ 2008 and it specifies that I should use scanf_s instead, it functions like scanf and I have'nt had any issues with it over the past few months. I'm not exactally sure why considering all programs I've seen still use scanf, but it gives me warnings stating things about possible incompatibility with newer compilers using scanf. In any case for the sake of arguement, I tested with both to see if it is just being problematic.
If you don't really see any issues with my setup, it could very well be just a compiler bug that no one has yet encountered.
Your password scan string is making me uncomfortable --- why arent you using the c++ string containers ?
eg,
1 2
std::string = password;
cin >> password;
etc.
What happens if the user inputs a password of length longer than 8 chars in your setup ?
Logic wise the program seems to be correct. Have you also tried printing out the read in password to see it is being read correctly ?
Also, you can remove the do-while loop for testing, the bug lies elsewhere ...
I tried your code in MS VC++ and it calls unhandled exception during debugging. However after removing line 12 ( scanf_s("%s",password); ) it seems to be OK.
There must be some error in using that scanf_s() function. Although I don't know this function its syntax is probably different from scanf() so you can't just simply change the names.
I suggest either to use cin (C++ syntax) or scanf (C syntax).