when i try to run the code am having problem in the output.
when the inputs s= 1, m=1 then am able to output i.e. solar and mains contactors result.
when both inputs are 0 i.e. s =0 and m = 0, then it is giving “There is no power”.(correct output)
when s = 1, m = 0 it should say “There is no power”. and am able to get the correct output.
when s=0, m=1, it should say “There is no power”, but instead its continuing to the solar contactors information(not the correct output).
So what could be the problem can anyone explain me?
#include<stdio.h>
#include<stdbool.h>
#define TRUE 1
#define FALSE 0
int main()
{
bool s, m; //input parameters
bool a, b;
bool p, q;
bool t; //output parameters
printf("Enter the value of solar VMD : \n"); //scanning vmd values of solar
scanf("%d", &s);
printf("Enter the value of Mains VMD : \n"); //scanning vmd values of Mains
scanf("%d", &m);
if(s == 1,m == 1)
{
printf("Scan solar contactor : \n"); //scanning solar contactor
scanf("%d", &a);
printf("Scan Mains contactor : \n"); //scanning mains contactor
scanf("%d", &b);
if(a == 1, b == 1) //when solar & mains contactor are close
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 0, b == 1) //when solar contctor is open and mains is closed
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 1, b == 0) //when solar contactor is closed and mains is open
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else if(a == 0, b == 0) //when both solar and mains are open
{
q = FALSE;
p = TRUE;
printf("Solar contactor and Mains contactor: %d %d", p, q);
}
else
{
printf("Problem with contactors");
}
}
else
{
printf("There is no power");
}
getchar();
getchar();
return 0;
}
This code doesn’t do what you think it does. Learn about operators of the C language, especially logical operators and the comma operator.
Update
The other bug is that
%dwould not work withbool. In fact there’s noscanfformat which would. So you have to either stick toint(which is a perfectly good type for doing boolean operators), or write your own function to readbools from a stream.