Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if statement ignored c

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

int main(){
    float a;
    float b;
    float c;

    printf("this program will solve you any equation from this type: ax^2+bx+c=0.\n");
    printf("First of all enter a, b and c:\n");
    printf("a:"); scanf("%f", &a);
    printf("b:"); scanf("%f", &b);
    printf("c:"); scanf("%f", &c); 

    if (a==0){
        printf("now your equation is:\n");
        printf("%.1fx+", b); printf("%.1f=0\n", c);
        float x3 = -c/b;
        printf("the solution for this equation is: %f", x3);
    }
    else{
    printf("now your equation is:\n");
    printf("%.1fx^2+", a); printf("%.1fx+", b); printf("%.1f=0\n", c);

    float d = b*b-4*a*c;
    printf("yy");

    float x1;
    float x2;

    if(d > 0){
        printf("delta is greater than 0! the equation have 2 solutions!\n");
        float x1 = (-b+sqrt(d))/(2*a);
        float x2 = (-b-sqrt(d))/(2*a);
        printf("the first solution is: %.2f\n", x1);
        printf("the second solution is: %.2f\n", x2);
    }

    else if (d == 0)
    {
        printf("delta equals 0 the equation have a doubled solution!\n");
        float x1 = -b / 2*a;
        printf("the doubled solution is: %f", x1);
    }

    else if (d < 0)
    {
        printf("delta is less than 0! the equation have no solutions in R :(");
    }

    }
    return 0;   
}

it works perfectly when i run it in vs code terminal thing but when i open the batch file it exits after entering the value of c
edit: the code i posted is only the start and the remaining code is 100% correct because i test it many times in terminal in vs code and it works

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The file is correctly executed. When you open the batch file, the program reaches the end at the return 0 istruction (immediatly after you enter c). This operation is so fast that apparently is invisible, but the program is executed correctly. The reason why you can see it into the terminal, is because the terminal doesn’t clear the prints.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading