Why doesn't the if else statement work? Please help...
YESH.CPP
#include<stdio.h>
#include<conio.h>
void main()
{
long int numbers;
int a,b,c,d,e,f,g;
printf("enter the 7 digits:\n\n\n\n\n");
scanf("%ld", &numbers);
a=numbers/1000000;
b=numbers%1000000/100000;
c=numbers%100000/10000;
d=numbers%10000/1000;
e=numbers%1000/100;
f=numbers%100/10;
g=numbers%10;
clrscr();
{
if(a==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",a);
if(b==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",b);
if(c==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",c);
if(d==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",d);
if(e==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",e);
if(f==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",f);
if(g==1|2|3|4|5|6|7|8|9|0)
printf("the value is %d \n",g);
}
if (a>b&c&d&e&f&g)
printf("the largest number is %d:",a);
else if(b>a&c&d&e&f&g)
printf("the largest number is %d:",b);
else if(c>a&b&d&e&f&g)
printf("the largest number is %d:",c);
else if(d>a&c&b&e&f&g)
printf("the largest number is %d:",d);
else if(e>a&c&d&b&f&g)
printf("the largest number is %d:",e);
else if(f>a&c&d&e&b&g)
printf("the largest number is %d:",f);
else if(g>a&c&d&e&f&b)
printf("the largest number is %d:",g);
getch();
} Thread: issue with an if/else in YESH.CPP. 's suggestion to break complex logic into named variables is sound; the compiler evaluates Boolean expressions exactly, so surprising results almost always come from a small set of coding mistakes or type/initialization issues (see if statement - cppreference).
Common causes and quick checks:
if (x = 5) assigns 5 then tests it; use == for comparison.if (cond); { ... } makes the block run unconditionally.if/else ambiguity can attach else to the wrong if.== on char* compares addresses, not contents.bool variables for clarity.if vs else if: use else if when branches must be exclusive.Practical debugging checklist: compile with warnings enabled (-Wall -Wextra), inspect values with a debugger or cerr, and add asserts for invariants. A small pattern that improves readability is to compute subconditions first and then make the decision:
bool condA = (x == 5);
bool condB = (ptr != nullptr && ptr->value > 0);
if (condA && condB) {
// both true
} else if (condA) {
// only A
} else {
// neither
} These steps make subtle bugs easier to spot and connect directly to 's advice about comparing one condition at a time.
Jump to Post— Topi Ojala 2Creating such if-else logics is tricky. Remember you're talking to a compiler through C++, not another person. The compiler will not be able to generate the correct true or false you want from your conditions.
I suggest you approach the problem a different way, perhaps through a variable which …
Creating such if-else logics is tricky. Remember you're talking to a compiler through C++, not another person. The compiler will not be able to generate the correct true or false you want from your conditions.
I suggest you approach the problem a different way, perhaps through a variable which you then compare one at a time( it's not a lot of code if you do it the smart way )
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.