Thread: Program closes without finishing command

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    1

    Program closes without finishing command

    When i am in codeblocks programme works perfectly fine but when i run the standalone exe after you enter first number and then value of the sides it just closes the programme. Thanks in advance for help. Its like it doesnt do the bolded code, just closes after scanf.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
    int brojFormule;
    float a, b, c;
    printf("\n\n\nUnesi broj za formulu koju zelis da koristis.\n");
    printf("\n1.Pitagorina teorema\n2.Povrsina pravouglog trougla\n3.Obim trougla\n4.Povrsina kvadrata\n5.Obim kvadrata\n6.Povrsina pravougaonika\n7.Obim pravougaonika\n\n");
    scanf("%d", &brojFormule);
    
    
    if (brojFormule == 1)
        {
        printf("\nUnesi vrednosti za stranice a i b:");
        scanf("%f%f", &a, &b);
            printf("c^2=%f\tc=%f\n", pow(a, 2)+pow(b, 2), sqrt(pow(a, 2)+pow(b, 2)));
        }
    else if (brojFormule == 2)
        {
            printf("\nUnesi vrednosti za stranice a i b:");
            scanf("%f%f", &a, &b);
                printf("P=%f\n", (a*b)/2);
        }
    else if (brojFormule == 3)
        {
            printf("\nUnesi vrednosti za stranice a,b,c:");
            scanf("%f%f%f", &a, &b, &c);
                printf("O=%f\n", a+b+c);
        }
    else if (brojFormule == 4)
        {
            printf("\nUnesi vrednost stranice a:");
            scanf("%f", &a);
                printf("P=%f\n", pow(a, 2));
        }
    else if (brojFormule == 5)
        {
            printf("\nUnesi vrednost stranice a:");
            scanf("%f", &a);
                printf("O=%f\n", 4*a);
        }
    else if (brojFormule == 6)
        {
            printf("\nUnesi vrednosti za stranice a i b:");
            scanf("%f%f", &a, &b);
                printf("P=%f\n", a*b);
        }
    else if (brojFormule == 7)
        {
            printf("\nUnesi vrednosti za stranice a i b:");
            scanf("%f%f", &a, &b);
                printf("O=%f\n", 2*(a+b));
        }
    else{ printf("Uneli ste pogresan broj");}
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    783
    I presume by "run the standalone exe" you mean double-clicking the icon (as opposed to running it in a command prompt). In that case the console will disappear when the program ends. CodeBlocks is presuambly holding the console open for you after the program ends (if I remember, it printed some timing info).


    You can hold the console open just before the return in main by using a getchar(); Actually you will probably need two since the last scanf will have left a newline in the input buffer, so add something like this before the return:
    Code:
        printf("Press enter to close the console...");
        getchar();
        getchar();
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finishing Off My Program
    By Steveqb14 in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2014, 10:10 AM
  2. help finishing this program
    By j.ward91 in forum C Programming
    Replies: 16
    Last Post: 04-26-2012, 10:44 AM
  3. Replies: 4
    Last Post: 07-13-2011, 08:24 PM
  4. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  5. finishing up the sorting program
    By jk in forum C Programming
    Replies: 2
    Last Post: 03-19-2002, 07:43 PM

Tags for this Thread