why doesn't this work?

0 votes
asked Mar 30, 2019 by Diaa Anki (120 points)

#include <stdio.h> 

int main () 

{

 printf ("Give me three integers:\n"); 

int x1, x2, x3, a;char g; 

scanf ("%d%d%d", &x1, &x2, &x3); 

printf ("\nSelect one of the following letters to perform a logical operation:\n"); printf ("A\tLogical AND\nO\tLogical OR\n"); 

scanf ("%s", &g); 

switch (g) 

{ case 'A': { if (x1 && x2 && x3) 

a = 1; 

else 

a = 0; printf ("%d\tAND\t%d\tAND\t%d\t=\t%d\n", x1, x2, x3, a); 

break; } 

case 'O': 

{ if (x1 || x2 || x3)

 a = 1; 

else a = 0;

 printf ("%d\tOR\t%d\tOR\t%d\t=\t%d\n", x1, x2, x3, a); 

break; } } 

return 0; 

}

2 Answers

0 votes
answered Apr 2, 2019 by Ashish (140 points)

use

scanf ("%c", &g); 

0 votes
answered Apr 23, 2019 by Matthew Green (260 points)

try this

#include <stdio.h> 

int main () 

{

 printf ("Give me three integers:\n"); 

int x1, x2, x3, a;char g; 

scanf ("%d%d%d", &x1, &x2, &x3); 

printf ("\nSelect one of the following letters to perform a logical operation:\n"); printf ("A\tLogical AND\nO\tLogical OR\n"); 

scanf ("%s", &g); 

switch (g) 

{ case 'A': { if (x1 && x2 && x3) 

a = 1; 

else 

a = 0; printf ("%d\tAND\t%d\tAND\t%d\t=\t%d\n", x1, x2, x3, a); 

break; } 

case 'O': 

{ if (x1 || x2 || x3)

 a = 1; 

else a = 0;

 printf ("%d\tOR\t%d\tOR\t%d\t=\t%d\n", x1, x2, x3, a); 

break; } } 

return 0; 
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...