I made a simple (and ugly) program to check the password that user inputs for uppercase, lowercase, number and the dollar sign. the problem is that my printf in the beginning of my do while loop repeats itself awkwardly while the scanf doesn't even stop it, and that repetition depends on the input of the user. i put some comments to explain the code, and i'll do some screen shots to show you what i mean by printf repeating itself.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
int main(){
char pass[10];
int i,t,x,y,z;
t=y=z=0;
do{
printf("Enter a password it needs to have an uppercase letter a lowercase one, a number and a $ sign: ");
scanf(" %c", pass);
//here i test for upper case, letter by letter
for(i=0;i<10;i++){
if(isalpha(pass[i])){
if(isupper(pass[i])){
t++;
}
}
}
//here i test for lower case, letter by letter
for(i=0;i<10;i++){
if(isalpha(pass[i])){
if(islower(pass[i])){
y++;
}
}
}
//here i test for number, letter by letter
for(i=0;i<10;i++){
if(isdigit(pass[i])){
z++;
}
}
//here i look for the dollar sign
for(i=0;i<10;i++){
if(pass[i]=='$'){
x++;
}
}
printf("\n");
//if all the tests were true the variables t,x,y and z would not be 0
}while(t==0 || x==0 || y==0 || z==0);
}
the first display of the code works fine: enter image description here
however as you will see in this picture, when i enter a bad password the printf repeats: enter image description here
and the repeating of printf depends on how much the user enters: enter image description here
even when i enter a good password, the printf repeats itself a bunch of times before the code ends: enter image description here
i assume the problem is with the for loops in bedded in the do while loop, but i just can't find where exactly is the problem and why printf repeats so weirdly
scanf(" %c", pass);is asking for achar.%sis the code for a string. Also, put a size limit on itscanf("%9s", pass);('9' because you have to leave space for end-of-string char'\0')for(i=0;i<10;i++){What if the user inputted string is less than 10 chars? Usestrlen()instead.scanf(" %c", pass)will read one character by one. So it will repeat printf for each car. Usescanf("%s", pass)to read a string.for (i=0; pass[i]; i++) {.