-6

This is what i have so far which does not work

int password [4] ;

int temp [4] ;

printf("Enter password : ") ;

Scanf ("%d" , &temp) ;


if (password.lenght == temp.lenght ) {

printf("The password is correct") ;

}

else {

printf ("Try Again");

}
CinCout
  • 9,486
  • 12
  • 49
  • 67
Dstk_1998
  • 1
  • 3

3 Answers3

1

Try using strings instead.

char password[4] = "abc";
char temp[4];

scanf("%s", temp);

Now, this will give you the string length:

size_t temp_len = strlen(temp);
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
1

I believe, that what you are trying to do is to compare if given number is the one stored in password. You don't need an array for storing a number. You would need one if your password would have been a string.

To check if two numbers a, b have the same number of digits simply divide both of them by 10 in loop and check if they hit 0 in the same moment.

xenteros
  • 15,586
  • 12
  • 56
  • 91
  • 1
    Alternatively, this will tell you if they have unequal digits: `(a / b >= 10) || (b / a >= 10)` – Mateen Ulhaq Jul 18 '16 at 10:15
  • No i am trying to check if the two arrays have the same length , It is for an ATM program I have to verify if the pin entered is a four digit number – Dstk_1998 Jul 18 '16 at 11:30
  • If it is as you wrote in this comment, you aren't comparing arrays - just numbers. You might got confused because of String representation as `char[]`. Three digit number as well as four digit number will be stored in a variable which type is `int`. – xenteros Jul 18 '16 at 11:52
  • For checking if a number is 4 digit just compare if it's greater then 999 and smaller than 10000. – xenteros Jul 18 '16 at 11:53
  • so i am using two variables for the whole password verification loop first one is password second one is tempPassword , what should the password be set to for this to work . Or should i only use one variable ? – Dstk_1998 Jul 18 '16 at 12:36
  • int password = 1234; in this case, 1234 would be acceptable PIN. int temp; printf("Enter password : ") ; Scanf ("%d" , &temp) ; if (password == temp) { //ok } else {wrong pin} – xenteros Jul 18 '16 at 12:37
  • It works if it's greater than 999 and smaller than 10000 as long as the first digit is not 0 , is there anyway to fix this loop condition so that the first digit of the pin could also be 0 – Dstk_1998 Jul 18 '16 at 21:46
  • Add leading 1 to both numbers – xenteros Jul 19 '16 at 06:31
1

Based on the comments to other answers, you want to check that someone has entered a four-digit string. The easy way to do this is to use an array of char to store the input, and to use strlen to get the length of the input string:

#include <stdio.h>
#include <string.h>
...
char input[5];  // extra space for string terminator
printf( "Enter password: " );
if ( scanf( "%4s", input ) != 1 ) // read no more than four characters
{
  // error on input
}
else if ( strlen( input ) < 4 )
{
  // password too short
}
else
{
  // check password value
}
alk
  • 69,737
  • 10
  • 105
  • 255
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • how do i verify that the pin number that is entered is an integer – Dstk_1998 Jul 18 '16 at 12:43
  • Why is array size 5 ? – Dstk_1998 Jul 18 '16 at 21:58
  • @Dstk_1998: C-"strings" always carry a trailing character, the so called `0`-terminator to mark the end of the "string". It they don't they aren't "strings", but just ordinary `char`-arrays. – alk Jul 21 '16 at 13:42
  • @Dstk_1998 "*how do i verify that the pin number that is entered is an integer[?]*" That's another question. – alk Jul 21 '16 at 13:44