-2

I want to be able to request the entry of 4 digits between 0 and 1 but when I do the test it still asks me to enter the screen despite entering 1001 or others.

#include <stdio.h> 
int main(void) { 
    int n,n1,n2,n3,mod0,mod1,mod2,mod3;

    do {//1025
            printf("Ingresar un numero de 4 digitos: "); scanf("%d",&n);
            
            n1 = n/10; //<- 102
            mod0 = n%10; // <- mod 5
            n2 = n1/10; // <- 10
            mod1 = n1%10; // <- mod 2
            n3 = n2/10; // <- 1
            mod2 = n2%10; // <- 0
            mod3 = n3%10; // 1
            
            if  ((mod3 != 1 || mod3 != 0) && ( mod2 !=1 || mod2 != 0) && (mod1 != 1 || mod1 != 0) && (mod0 !=1 || mod0 != 0)){
                    printf("Ingresar digitos binarios\n");
            }
    }while((mod3 != 1 || mod3 != 0) && ( mod2 !=1 || mod2 != 0) && (mod1 != 1 || mod1 != 0) && (mod0 !=1 || mod0 != 0));
    
    return 0; 

}

Lion
  • 1
  • 3
    You are using `||` and `&&` backwards. Sit down and evaluate the `if` control expression "by hand". Start with the whole expression, substitute the actual values of `mod3`, `mod2`, etc. Then perform each `!=` replacing the operation with `true` or `false`. Then look at each parenthesized `||`, following the truth table for `||`. Be careful to evaluate the code you actually wrote, and not jump directly to what you wanted it to mean. – Ben Voigt Aug 17 '22 at 22:13
  • You might also benefit from adding more temporary variables. Maybe one of them could be named `mod3_is_bad` or `reject_mod3`. What expression would you set that variable to? When you step through with a debugger, does that get the true/false value you hoped for? – Ben Voigt Aug 17 '22 at 22:16
  • 3
    Easier to read a string and make sure it's 4 characters long and only has those 2 characters in it... `if (strspn(str, "01") == 4 && str[4] == '\0') { /* It's good */ }` – Shawn Aug 17 '22 at 22:31
  • Similar: https://stackoverflow.com/questions/69228173/way-to-scanf-binary-number – Computable Aug 18 '22 at 00:03
  • sorry im new to c I was analyzing the auxiliary variable mod3_is_bad, reject_mod3 but I have no idea where to put it and how it would help me. also changing them || and & now it works for me, but I realize that even if I put 101010101010101010 it still takes it as valid, how could I control it? – Lion Aug 18 '22 at 08:18

1 Answers1

0

It keeps asking for digits, since you have the scanf inside the loop. Placing printf("Ingresar un numero de 4 digitos: "); scanf("%d",&n); above your do-while loop will solve your problem.

Also, you should look at the conditions of your loop. Isn’t it easier to loop until n == 0?

Example:

  1. n = 1025, mod = 0
  2. while n != 0:
  3. mod = n % 10 (makes 5)
  4. n = n / 10 (makes 102)
  5. Check if mod is 0 or 1
  6. repeat for next digits

In the example you will seperate each last digit from n and check whether it is a 0 or 1.

EDIT: This link explains what I want to show with the example. Instead of printing the digits, you should check if it is a 0 or 1.

BJKoop
  • 11
  • 4