Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Salem

stuck on scanf ?????

This is a discussion on stuck on scanf ????? within the C Programming forums, part of the General Programming Boards category; Hi... i program signal handler like this ? Code: void sig_quit(int signo) { char ans[5]; int check = 0; printf("Are ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    20

    Question stuck on scanf ?????

    Hi...
    i program signal handler like this ?

    Code:
    void
    sig_quit(int signo)
    {
        char ans[5];
        int check = 0;
        
                    printf("Are you sure want to exit program ?Y/N \n");
                    //while(check){ check = !scanf("%s", ans); } 
                    while(check!=1)
                    {
                        scanf("%s", ans);
                        printf("ans is : %s",ans);            
                        if(ans[0]=='y' ||  ans[0]=='Y')
                        {                                    
                            printf("Goodbye\n");
                            exit(0);
                            return;
                        }
                        else if(ans[0]!='y')
                        {    
                            check=1;
                            printf("why ans is : %s",ans);
                            continue;                                    
                        }    
                    }
                                                                        
                                    
    }
    ok the default behavior is when you press ctrl+\ the
    question ask you "are you sure to exit ?
    if the user press 'y' it will exit ,but if the user press anything else other than 'y' -->it will stuck on scnaf again,but i need to exit the function and return to main after the user press 'n' how ???

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    https://www.securecoding.cert.org/co...ignal+handlers
    printf/scanf functions are NOT safe to use in signal handlers.
    stahta01 likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    India
    Posts
    2

    i guess buffer problem

    may be there is some buffer problem. try cleaning buffer using fflush(stdin);
    moreover you are just taking ans in 1 character but you are taking a string as an input, you are wasting resources dude.
    use:

    char choice;
    scanf("%c",&choice);
    or
    choice = getche();

    the last continue statement is just a waste. it is not doing anything. you can remove it.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    20

    Replay... with new code

    this is modification but the same problem it stucks @ getchar() or scanf(), again after inserting any char except 'y' ,promting it for new input ,after it stuck if the user insert any new char it will exit the signal handler function back to the main()

    CODE:

    Code:
    sig_quit(int signo)
    {
        //char ans[5];
        char ans;
        
                    printf("Are you sure want to exit program ?Y/N \n");
                    //while(check){ check = !scanf("%s", ans); } 
                    
                    //scanf("%c",&ans);
                    ans=getchar();
                        //fgets(ans,sizeof(ans),stdin);;
                        printf("ans is : %c",ans);            
                        if(ans=='y' ||  ans=='Y')
                        {                                    
                            printf("Goodbye\n");
                            exit(0);
                            return;
                        }
                        else if(ans!='y')
                        {    
                            
                            printf("why ans is : %c",ans);
                                                            
                        }    
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    I guess I wasted my time saying anything at all....
    AndiPersti likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stuck with scanf %s
    By pipskie in forum C Programming
    Replies: 6
    Last Post: 01-21-2013, 08:02 AM
  2. Stuck and need help.
    By tvred in forum C++ Programming
    Replies: 4
    Last Post: 03-23-2010, 11:34 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. please help, i'm really stuck
    By sweetly in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2003, 03:57 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21