Thread: If-else loop issues

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    2

    If-else loop issues

    Hi, I am a newbie to C, and I am having issues while running the following code:

    Code:
    #include<stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define rho_l 1000
    
    
    int main()
    {
        float cw,eta,rho_m,rho_s,D_i;
        float Re_b,tao_0,He,V;
        float fnl,a,b,fnt;
        float fn,m,fd,Pd_l;
        char ch,ch1;
    
    
        printf("\nIs the slurry density known?(Y/N) ");
        scanf("%c",&ch);
    
    
        if (ch == 'Y' || ch == 'y')
        {
            printf("\nEnter slurry density: ");
            scanf("%f",&rho_m);
        }
        else //if (ch == 'N' || ch == 'n')
        {
            
            printf("\nIs solid density known?(Y/N) ");
            scanf("%c",&ch1);
            printf("%c is value of ch",ch1);
            if (ch1 == 'Y' || ch1 == 'y')
            {
                printf("\nEnter solid density: ");
                scanf("%f",&rho_s);
                printf("\nInput Slurry Concentration: ");
                scanf("%f",&cw);
                rho_m = 100/((cw/rho_s)+((100-cw)/rho_l) );            //Calculate Slurry density
            }
            else if (ch1 == 'N' || ch1 == 'n')
            {
                printf("\nInsufficient data: ");
                exit(0);
            }
            else
            {
                printf("\nInvalid Input!");
                exit(0);
            }
        }
        else
        {
            printf("\nInvalid Input!");
            exit(0);
        }
    
    
    
    
        printf("\nInput Pipe Diameter: ");
        scanf("%f",&D_i);
    
    
        printf("Input flow velocity: ");
        scanf("%f",&V);
    
    
        printf("Input yield stress: ");
        scanf("%f",&tao_0);
    
    
        printf("Input Slurry Viscosity: ");
        scanf("%f",&eta);
    
    
        // Calculate Reynold's number for BinghamPlastic flow
        Re_b = (D_i*V*rho_m)/eta;
        printf("\n %f is the Reynold's number",Re_b);
    
    
        // Calculate Hedstrom number
        He = (pow(D_i,2.0)*rho_m*tao_0)/pow(eta,2.0);
        printf("\n %f is the Hedstrom's number",He);
    
    
        //Calculate friction factor in laminar regime
        fnl = (16/Re_b)*(1 + Re_b/(6*He));
        printf("\n %f is the laminar friction factor",fnl);
    
    
        //Calcualte friction factor in turbulent regime
        a = -1.47*(1+0.416*exp(-2.9*pow(10,-5)*He));
        b = -0.193;
        fnt = pow(10,a)*pow(Re_b,b);
        printf("\n %f is the turbulent friction factor",fnt);
        printf("\n %f is a",a);
    
    
        //Calcualte overall Fanning friction factor
        m = 1.7 + 40000/Re_b;
        fn = pow((pow(fnl,m)+pow(fnt,m)),1/m);
        printf("\n %f is m",m);
        printf("\n %f is the overall friction factor",fn);
    
    
        //Calcualte Darcy friction factor
        fd = 4*fn;
        printf("\n %f is the Darcy friction factor",fd);
    
    
        //Calculate pressure drop per unit length
        Pd_l = (rho_m*fd*pow(V,2.0))/(2*D_i);
    
    
        printf("\n %f is the pressure drop in Pa/m",Pd_l );
        return 0;
        getch();
    }
    When I chose 'Y' for the first question
    Code:
    Is the slurry density known?(Y/N)
    then the program works well, but when I chose 'N' then the program prints
    Code:
    Is solid density known?(Y/N)
    and then
    Code:
     Invalid Input!
    . It does not take any input from me in that case.

    The code does not go into the second if-else loop. How do I solve this problem?
    Thanks...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,036
    > scanf("%c",&ch1);
    The %c format is peculiar in that it doesn't do the "skip whitespace and newlines" that almost all the other formats do.

    If you want the next actual character such as Y or N, then
    Code:
    // add a leading space to the format.
    scanf(" %c",&ch1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    2
    Thank you, this helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop issues
    By rklein in forum C Programming
    Replies: 5
    Last Post: 02-10-2017, 08:05 PM
  2. Loop Issues
    By monekychef in forum C Programming
    Replies: 10
    Last Post: 04-22-2013, 10:55 PM
  3. While loop issues
    By levitylek in forum C Programming
    Replies: 3
    Last Post: 10-14-2010, 05:29 PM
  4. For Loop Issues
    By jtkhoskinson in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2010, 01:28 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

Tags for this Thread