Home > Enterprise >  Issues with do while loop
Issues with do while loop

Time:12-08

The code allows you to choose between star and delta resistive network conversions. There is also an exit option. I wanted to validate the values for R_a, R_b, R_c etc. however, I have run into some trouble with my do while loop. The lower limit is 1000 and the upper is 1000000.

I intend to have the program carry on if the input is within range and prompt for another input from the user if it is not. However, as of now, the program continues if the value is within range, but also continues after giving a warning when it is not - when I want it to loop back to the first input prompt.

Once correct, I will add the loop to all inputs. If anyone is able to fix/find the issue, it would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    printf("\n\n\t\tDelta and Star Converter\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Star \n");
        printf("2. Delta\n");
        printf("0. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);



        switch(choice)
        {
        case 1:;

        float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;



        printf("Please enter the value of the Star connected resistors:\n");

          do {
        printf("R_a = ");
         scanf("%f",&R_a);
        if (R_a > 1000000) {
            printf("Please");
        } else if (R_a < 1000) {
            printf("Number to low\n");
        }else {
        }


     }while(R_a = -0);





        printf("R_b = ");
        scanf("%f",&R_b);
        printf("R_c = ");
        scanf("%f",&R_c);


        R_ab=R_a R_b (R_a*R_b)/R_c;
        R_bc=R_b R_c (R_b*R_c)/R_a;
        R_ac=R_a R_c (R_a*R_c)/R_b;

        printf("the equivalent Delta values are: \n");
        printf("R_ab = %.2f Ohms\n",R_ab);
        printf("R_bc = %.2f Ohms\n",R_bc);
        printf("R_ac = %.2f Ohms\n",R_ac);
        break;

        case 2:;



        printf("Please enter the values of the Delta connected resistors:\n");

        printf("R_ab = ");
        scanf("%f",&R_ab);
        printf("R_bc = ");
        scanf("%f",&R_bc);
        printf("R_ac = ");
        scanf("%f",&R_ac);



        R_a = (R_ab*R_ac)/(R_ab   R_bc   R_ac);
        R_b = (R_ab*R_bc)/(R_ab   R_bc   R_ac);
        R_c = (R_ac*R_bc)/(R_ab   R_bc   R_ac);

        printf("the equivalent Star values are: \n");
        printf("R_a = %.2f Ohms\n",R_a);
        printf("R_b = %.2f Ohms\n",R_b);
        printf("R_c = %.2f Ohms\n",R_c);
        break;

            case 0:
                printf("\n\nAdios!!\n\n\n");
                exit(0);    // terminates the complete program execution
        }
while (0) ;  }
    printf("\n\n\t\t\tThank you!\n\n\n");
    return 0;
}

CodePudding user response:

while(R_a = -0)

= is the assignment operator. This assigns -0 to R_a, and evaluates to the same falsy value, ending the loop.

Change the do ... while to an infinite loop, and break the loop when the value is in range.

while (1) {
    printf("R_a = ");

    if (1 != scanf("%f", &R_a))
        exit(EXIT_FAILURE);

    if (R_a > 1000000) {
        fprintf(stderr, "Number too high.\n");
    } else if (R_a < 1000) {
        fprintf(stderr, "Number to low.\n");
    } else {
        break;
    }
}
  • Related