Home > Net >  Code executes all cases even with break statement
Code executes all cases even with break statement

Time:12-30

This a simple program to rotate bits, the switch case used to define if the user wants a left rotating or right rotating after getting the most significant bit or the least significant bit depending on the choice I used for loop in side of switch and I used the same values and It execute all cases no mater what I entered.

#include <stdio.h>
#define SIZE sizeof(number)*8

void main()
{
    int number, rotate, i, SB, numberr;
    char choice;

    puts("simple program to rotate bits of a number");
    printf("If you want right rotation type 'R'\n");
    printf("enter your number : ");
    scanf("%d", &number);
    printf("Enter how much time you want to rotate : ");
    scanf("%d", &rotate);
    getchar();
    printf("IF you want left rotation type 'L' : ");
    scanf("%c", &choice);

    switch(choice)
        {
        case 'l' :
        case 'L' :
            SB = (1<<(SIZE-1))&1;
            for(i=0;i<rotate;i  )
                {
                numberr = (number << 1) | SB;
                printf("\n%d after being rotated %d times became %d\n\n", number, rotate, numberr);
                }
            break;
        case 'r' :
        case 'R' :
            SB = number & 1;
            for(i=0;i<rotate;i  )
                {
                numberr = (number >> 1) | SB;
                printf("\n%d after being rotated %d time became %d\n\n", number, rotate, numberr);
                }
            break;
        default :
            printf("please chose either R or L\n\n");
        }
}

CodePudding user response:

The output is confusing because the messages from each case are identically phrased. When I added L and R to the message, it showed that only one case is executed.

The problem is that the output is within the loop, and needs to be moved outside, for example

    case 'l' :
    case 'L' :
        SB = (1<<(SIZE-1))&1;
        for(i=0;i<rotate;i  )
            {
            numberr = (number << 1) | SB;
            }
        printf("L %d after being rotated %d times became %d\n\n", number, rotate, numberr);            
        break;

CodePudding user response:

I have refined your code and this might help. You might want to improve the syntax of your code before posting the question.

#include <stdio.h>
#define SIZE 32

int main() {
    int number, rotate, i, number1;
    char choice;
    int output;

    puts("simple program to rotate bits of a number");

    printf("enter your number : ");
    scanf("%d", &number);
    printf("Enter how much time you want to rotate : ");
    scanf("%d", &rotate);
    printf("If you want right rotation type 'R'\n");
    printf("IF you want left rotation type 'L' : ");
    scanf(" %c", &choice);

    switch(choice) {
        case 'l' :
        case 'L' :
            output = (number << rotate) | (number >> (SIZE - rotate));
            printf("%d after rotating %d times became %d,",number,rotate,output);
            break;
        case 'r' :
        case 'R' :
            output = (number >> rotate) | (number << (SIZE - rotate));
            printf("%d after rotating %d times became %d,",number,rotate,output);
            break;
        default :
            printf("Please choose either R or L\n\n");
    }
    return 0;
}
  • Related