Thread: After scanf terminal closes automaticly, whats the problem?

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    5

    Question After scanf terminal closes automaticly, whats the problem?

    Hi, i'm new to C, and I cannot find the problem, is it somesort of text input failure or what?

    Here is the whole program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <windows.h>
    #include <limits.h>
    #include <string.h>
    int pirma();
    
    
    int main()
    {
        //goto START;
    START:
        ;
        system("cls"); //clear screen
        int a=INT_MAX;
        printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n");
        printf("|         Programos veiksmu pasirinkimas:         |\n");
        printf("|irasykite skaiciu, kurios programos dalies norite|\n");
        printf("///////////////////////////////////////////////////\n");
        printf("|1 - is dvejetaines i desimtaine                  |\n");
        printf("|2 - is dvejetaines i sesioliktaine               |\n");
        printf("|3 - is desimtaines i dvejetaine                  |\n");
        printf("|4 - is desimtaines i sesioliktaine               |\n");
        printf("|5 - is sesioliktaines i dvejetaine               |\n");
        printf("|6 - is desioliktaines i desimtaine               |\n");
        printf("|0 - iseiti is programos                          |\n");
        printf("\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n");
        scanf("%d", &a);
    
    
        system("cls"); //clear screen
    
    
        //start of warning if they choose a program that do not exist
        if(a>6)
        {
            int b=0;
            printf("Nera tokios programos\n");
            printf("Paspauskite 1, kad galetumete is naujo pasirinkti\n");
            scanf("%d", &b);
            if(b==1)
            {
                system("cls"); //clear screen
                goto START;
            }
            int c=INT_MAX;
            while(c==INT_MAX)
            {
                printf("Paspauskite 1, kad galetumete is naujo pasirinkti\n");
                scanf("%d", &b);
                if(b==1)
                {
                    c=1;
                    system("cls"); //clear screen
                    goto START;
                }
            }
        }
        //end of warning if they choose a program that do not exist
    
    
        //nuline, jei nori iseiti is programos
        if(a==0)
        {
            char p, y="y";
            int lygybe=0;
            printf("Ar tikrai norite iseiti is programos?\n");
            printf("Jei taip, spauskite y\n");
            printf("Jei ne, spauskite n\n");
            scanf("%c", &p);
    
    
            lygybe=strcmp(p, y);
            if(lygybe==0)
                exit(0);
            else
                goto START;
        }
    
    
        //pirmoji programa
        if(a==1)
        {
            pirma();
        }
    
    
        return 0;
    }
    
    
    int pirma()
    {
        int binary=0, bp=0; //bp - isaugojimas pradiniu duomenu
        int decimal=0;
        int k=0; //pagalbinis skaiciuojant desimtaini
        int m=1; //kitas pagalbinis, kuris dides
        printf("***********************************************************************************\n");
        printf("**  Iveskite savo norima dvejetaini koda, kuri noretumete paversti desimtainiu:  **\n");
        printf("***********************************************************************************\n");
        scanf("%d", &binary);
        bp = binary;
    
    
        while (bp > 0)
        {
            k = bp % 10;
            decimal=decimal+k*m;
            bp=bp/10;
            m=m*2;
        }
        system ("cls");
        printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n");
        printf("HH  Jusu pasirinktas dvejetainis kodas: %d", binary);
        printf("                     HH\n");
        printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n");
    
    
        printf("HH  Gautas desimtainis kodas, konvertutoas is dvejetainio: %d", decimal);
        printf("  HH\n");
        printf("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n");
    }
    But the problem comes when in the start you press 0, and thats where comes scanf I meantioned before
    Code:
    if(a==0)
        {
            char p, y="y";
            int lygybe=0;
            printf("Ar tikrai norite iseiti is programos?\n");
            printf("Jei taip, spauskite y\n");
            printf("Jei ne, spauskite n\n");
            scanf("%c", &p);
    
    
            lygybe=strcmp(p, y);
            if(lygybe==0)
                exit(0);
            else
                goto START;
        }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,463
    > lygybe=strcmp(p, y);
    If your compiler isn't complaining about this, get a better compiler.

    If it is complaining, but you're choosing to ignore it, then be a better student.
    Code:
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:64:19: warning: initialization of ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
       64 |         char p, y="y";
          |                   ^~~
    bar.c:72:23: warning: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
       72 |         lygybe=strcmp(p, y);
          |                       ^
          |                       |
          |                       char
    bar.c:72:26: warning: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast [-Wint-conversion]
       72 |         lygybe=strcmp(p, y);
          |                          ^
          |                          |
          |                          char
    Since you're only inputting a single character, do this.
    Code:
            char p, y='y'; // note single quotes for single letters assigned to char
            int lygybe=0;
            printf("Ar tikrai norite iseiti is programos?\n");
            printf("Jei taip, spauskite y\n");
            printf("Jei ne, spauskite n\n");
            scanf(" %c", &p); // NOTE!! single space before %c
    
            if ( p == y )
    The space in the %c format is important, it makes sure you read the next visible character.

    > #include <conio.h>
    > #include <windows.h>
    You don't need either of these for console programs.


    Another point, backward goto jumps should really be replaced with a while loop.
    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
    Oct 2022
    Posts
    5
    Thank you very much, and my compiler doesn't say anything
    But I will try to be...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-28-2011, 09:01 PM
  2. Program closes when startup form closes
    By dcboy in forum C# Programming
    Replies: 1
    Last Post: 07-01-2006, 02:40 AM
  3. [ HELP ] automaticly open an url
    By The0ne in forum C++ Programming
    Replies: 5
    Last Post: 10-16-2002, 05:24 PM
  4. need a program that moves the crouser autOmaticly
    By yaniv in forum Windows Programming
    Replies: 23
    Last Post: 07-23-2002, 08:05 PM
  5. Replies: 2
    Last Post: 07-14-2002, 09:08 PM

Tags for this Thread