0

I'm making a simple program that returns the sum of the sizes of user input. User first inputs an int for amount of sizeof() sums to add Followed by an input and char for data type. This all works, BUT, what fails is if user types in anything other than 'c', 'd' or 'i' loop breaks and prints "invalid tracking code type."

My program fails on the conditional - even though the scanf() correctly obtains the char, it still fails.

FOR EXAMPLE:

INPUT:

3
10 i
7 c
12 d

OUTPUT

143 bytes

INPUT

1
1 o

OUTPUT

invalid tracking code type

how can you compare a user input using scanf in a loop?

#include <stdio.h>

int main()
{
    int num, input, i, sum;
    int invalid = 1;
    sum = 0;
    i = 0;
    char type;

    printf("give me a num!\n");
    scanf("%d", &num);

    while(i < num)
    {
        printf("int and char?\n");
        //scanf("%d", &input);
        //scanf(" %c\n", &type);
        if (scanf("%d %c", &input, &type) != 2)
        {
            printf("TYPE IS: %c\n", type);


        }
        printf("TYPE IS: %c\n", type);
        if(type != 'c' || type != 'd' || type != 'i')
        {
            printf("invalid tracking code type");
            invalid = 0;
            break;
        }
        i++;
        //printf("what is i? %d\n", i);
        sum = (sizeof(input)) + sum;
        printf("IT's the sum %d\n", sum);

    }

    if(invalid != 0)
    {
    printf("%d bytes", sum);
    }

    return 0;
}

There are a number of other posts that answer similar issues, but wasn't able to find anything specifically for this issue.

I've tried a number of things such as separating the scanf(), using different types of operators for the conditional. It works fine without the conditional, but with the conditional it always returns "invalid tracking code type" even when they are correct.

Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • 5
    `type != 'c' || type != 'd' || type != 'i'` will always be true. – 1201ProgramAlarm Dec 26 '19 at 17:59
  • 1
    Also the check on `scanf` return value is *strange*. You actually seem to ignore it, since you print the value of the variable that will be for sure not set if return value is ` != 2`. – Roberto Caboni Dec 26 '19 at 18:15
  • 1
    regarding: `if (scanf("%d %c", &input, &type) != 2) { printf("TYPE IS: %c\n", type); }` That call to `printf()` should be a call to `fprintf( stderr, "some error message\n");` – user3629249 Dec 26 '19 at 18:26
  • 1
    regarding: `if(type != 'c' || type != 'd' || type != 'i')` This is not the correct logic. Suggest: `if(type != 'c' && type != 'd' && type != 'i')` – user3629249 Dec 26 '19 at 18:28
  • 1
    regarding: `sum = (sizeof(input)) + sum;` the variable `input` is an `int` which will always be (depending on the underlying hardware architecture) 4 or 8 – user3629249 Dec 26 '19 at 18:31
  • 1
    OT: for ease of readability and understanding: 1) please follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* 2) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Dec 26 '19 at 18:34
  • how do `7` + `10` + `13` add up to `143`? – user3629249 Dec 26 '19 at 18:39

1 Answers1

1

Replace

if(type != 'c' || type != 'd' || type != 'i')

By

if(type != 'c' && type != 'd' && type != 'i')
benjarobin
  • 4,410
  • 27
  • 21
  • I'm curious why the && operator in this case? I have more experience with java and intuitively if I think about what the || is asking it would be "if type is not 'c' OR 'd' OR 'i' then.... and using the && I'm thinking AND, so switching the "OR" with the "AND" would mean all 3 conditions would have to be met which seems un-intuitive – Katie Melosto Dec 26 '19 at 18:08
  • 1
    If "type" equal to 'c', then `type != 'd' ` is equal to "true". So the whole condition is true. In Java you have exactly the same result... And yes, "all 3 conditions would have to be met" – benjarobin Dec 26 '19 at 18:23
  • 1
    @KatieMelosto -- the case you are interested in is "NOT ('c' OR 'd' OR 'i')", which is logically equivalent to "NOT 'c' AND NOT 'd' AND NOT 'i'"; see [De Morgan's Laws](https://en.wikipedia.org/wiki/De_Morgan's_laws). – ad absurdum Dec 26 '19 at 18:34