Thread: stuck on scanf ?????

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    https://www.securecoding.cert.org/co...ignal+handlers
    printf/scanf functions are NOT safe to use in signal handlers.
    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
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I guess I wasted my time saying anything at all....
    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.

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, 09: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