(c language) Char character duplicate application error question.

Asked 1 years ago, Updated 1 years ago, 241 views

I'm studying coding to calculate the width by multiplying the base and height of the triangle and adding continue? at the end.

I tried to make it possible to leave the program if I put Y or Y after the continue and put another character.

    int base, height;
    char con;
    double width;

    while (1) {
        printf("Base = ");
        scanf_s("%d", &base);
        printf("Height = ");
        scanf_s("%d", &height);

        width = (base * height) / 2;
        printf("Triangle width = %.1lf\n",width);

        printf("Continue? ");
        scanf_s(" %c", &con);
        if (con != 'Y' || 'y') break;
    }

But what I want to do is leave the program when I type Y and y, but I can't implement both of them. Only Korean characters are implemented as above.

I wonder how I can get on the program if I put in other letters except Y and Y.

c

2022-10-25 00:00

2 Answers

if (con != 'Y' || 'y') break;

We need to change the line above as below.

if (con != 'Y' && con != 'y') break;

Escape when con is not 'Y' and con is not (&&)


2022-10-25 00:00

con!= 'Y' || 'y' is

However, 'y' is always interpreted as true. So the whole condition is always going to be true.


2022-10-25 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.