Reputation: 1
In the following code, whenever the same string is input in arrays "pass" and "repass", the string in "repass" gets doubled. For example, if the input string in "pass" and "repass" are aaaaaaaa then the string in "repass" becomes aaaaaaaaaaaaaaaa because of which strcmp()
gives a negative answer.
Can somebody help and explain the reason behind this?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char user_name[20],pass[8],repass[8];
int i=0,c=0,tr=1;//tr for no of try(should less than 3 )
clrscr();
puts("enter user name");
gets(user_name);
printf("\n\n\n\n");
for(tr=1;tr<=3;tr++)
{
puts("\n\nenter password");
while(i<8)
{
pass[i] = getch();
putchar('*');
i++;
}
printf("\n\n\n\nplease reenter the password\n\n");
i=0;
while(i<8)
{
repass[i]=getch();
putchar('*');
i++;
}
c=strcmp(pass, repass);
printf("c=%d", c);
if(strcmp(pass,repass)==0)
c=0;
else
c++;
if(c==0)
{
printf("\n\n\t****\vsuccessful login*********** ");
break;
}
else
printf("\n\nsorry password did not match");
}
if(tr>3)
puts("\n\nlogin failed");
//printf("%s %s",pass,repass);
getch();
}
Upvotes: 0
Views: 197
Reputation: 17131
There's a few things wrong here.
'\0'
at the end to signal that they're done.Upvotes: 1
Reputation: 182734
You're not 0-terminating your strings so using "string" functions on them (printing with "%s", strcmp, etc) is illegal.
In this particular case it looks as repass
is "doubled" because of the stack layout, the way pass
and repass
are one next to the other.
Side node, use fgets
instead of gets
.
Upvotes: 2