Rahul Subedi
Rahul Subedi

Reputation: 131

error in if condition even i enter code correctly in c programming

#include <stdio.h>
#include <process.h>

int main()
{
     int check;
     int enter[7];
     int i,j;

     printf("enter any 7 number to be stored");
     for(i = 0; i < 7; i++)
           scanf("%d" ,&enter[i]);
     printf("\nenter any number to check:");
     scanf("%d" ,&check);
     for (i = 0; i < 7; i++)
     {
           if (enter[i]=check)
           {
                printf("your entry is valid");
                exit(0);
           }
           else if(enter[6]!=check)
           {
                 printf("your entry is not valid");
                 exit(0);
           }
           else
                 continue;
     }
     return 0;
}     

this executes without error but dont work correctly .. always prints out the input is valid.... even i enter the number which is not in array :(

Upvotes: 0

Views: 227

Answers (6)

Rahul Subedi
Rahul Subedi

Reputation: 131

#include<stdio.h>
#include<process.h>
int main()

{
int check;
int enter[7];
int i,j;
printf("enter any 7 number to be stored");
for(i=0;i<7;i++)
{
    scanf("%d" ,&enter[i]);

}
printf("\nenter any number to check:");
scanf("%d" ,&check);
for (i=0;i<7;i++)
{
   // printf("\nvalue of i is %d\n" ,i);
    if (check==enter[i])
    {
        printf("your entry is valid");
        exit(0);
    }
    else if(enter[i]!=check && i==6)
    {

        printf("your entry is not valid");
        exit(0);
    }
    else
    continue;
}
return 0;
}

 now i got it all right . thanks :)

Upvotes: 0

jwara
jwara

Reputation: 11

proper usage of = is assignment operator whilst == is testing the equality

Upvotes: 0

QuentinUK
QuentinUK

Reputation: 3077

enter[i]==check)// 2 for compare

Upvotes: 0

md5
md5

Reputation: 23709

= is the assignment operator, not equality at all. Doing:

if (enter[i]=check)

enter[i] will take the value check, and then it will check whether enter[i] is nonzero.

if (enter[i] == check)

Upvotes: 0

hmjd
hmjd

Reputation: 121971

This is assignment, not equality:

if (enter[i]=check)

Change to:

if (enter[i] == check)

Additionally, always check the result of input operations:

if (1 != scanf("%d" ,&enter[i]))
{
    /* Handle invalid value. */
}

to ensure subsequent code is operating on variables that have been assigned values.

Upvotes: 4

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

This line

if (enter[i]=check)

does not do what you expect. You probably meant

if (enter[i]==check)

The assignment is valid C, but instead of checking for equality, it sets enter[i] equal check, and then checks the value of check for being zero. If it is non-zero, the condition succeeds, regardless of the initial value of enter[i]. If the check is zero, then the condition fails, - again, regardless of the initial value of enter[i]. This is a very common mistake; many compilers issue warnings to alert you to the situation.

Upvotes: 1

Related Questions