problem with C, pls helppppp with this!!!!!!!!!

Discussion in 'QnA (read only)' started by legolas, Mar 28, 2006.

Thread Status:
Not open for further replies.
  1. legolas

    legolas New Member

    Joined:
    Feb 8, 2005
    Messages:
    959
    Likes Received:
    0
    Trophy Points:
    0
    hi,

    pls tel me if there is any mistake in this code. I couldnt figure out the problem, in this program, the scanf statement scanf ("%c",&xit) doesnt get executed... i use FC4 linux and gcc 4.0 version. Pls tel me what is the problem or its a prb with linux???

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	int choice=0;
    	char xit;
    	printf("Enter Choice:\n 1) Create\n 2) Solve\n 3) exit\n");
    	scanf( "%d", &choice );
    	printf( "\n" );
    	if ( choice == 1 )
    		printf ( "yet to create\n" );
    	else 
    	{
    		printf ( "Do you really want to quit?" );
    		scanf ( "%c", &xit );	
    		if ( xit == 'y' )
    			exit(0);
    		else 
    			printf("continue...\n");
    	}
    	return(0);
    }
    
    o/p is:
    bash-3.00$ gcc sample.c
    bash-3.00$ ./a.out
    Enter choice
    1) create
    2) solve
    3) exit
    3

    Do you really want to quit: continue....
    bash-3.00$

    it is not asking for the input of xit itself!!! i also tried uysing xit = getchar(); that also dint work!! wats the problem.

    /legolas
     
  2. ilugd

    ilugd Beware of the innocent

    Joined:
    Dec 25, 2005
    Messages:
    1,009
    Likes Received:
    3
    Trophy Points:
    0
    you might want to change the way how you are geting the value of choice.

    Actually what is happening is that when scanf asks for choice the use enters 3\n (that is 3 and then hits the enter key)
    so scanf assigns 3 to choice and has \n on the input buffer (or wherever). On the next call, to xit, \n is assigned to xit

    you could use some other function to get a single key input instead of scanf or put another call to scanf before scanf(&xit) to get rid of that new line.

    Hope it helps
     
  3. sakumar79

    sakumar79 Active Member

    Joined:
    Nov 28, 2004
    Messages:
    2,441
    Likes Received:
    9
    Trophy Points:
    38
    Location:
    Madurai
    Why dont you clear the buffer before scanning for input? Also, do a loop until y/Y or n/N is input... Otherwise, for all characters except y, it will always quit... I think (its been a long time since I did C coding, so I might be wrong) that the enter pressed earlier during the previous scan command is in the buffer and used in this scanf....

    Arun
     
  4. mediator

    mediator New Member

    Joined:
    Mar 18, 2005
    Messages:
    2,491
    Likes Received:
    16
    Trophy Points:
    0
    Location:
    In ur Evil Mind!
    Hi i have lost my touch in c++, but If i'm right the problem is of the uncleared buffer i think! From the first scanf which takes integer value, the buffer is not cleared. So if u give some value like 10c or whatever...10 is taken in as integer value and the next 'c' after 10 is taken immediately in 2nd scanf!
    I think in ur case 2nd scanf is taking empty character as input automatically!
    There are a number of solutions to it,
    1) U can use the function gets() to the job
    2) Instead of %c, u shud try %s ...well if i'm right id doesnt have buffer problems with %s!
    3) There is a function flush() or flushall() check that out urself that clears buffers. So after every "scanf" statement u shud write that flush function! e.g
    scanf("%d",&n);
    flush fn.
    scanf("%c",&xit);
    flush fn.

    I hope this helps ya!
     
  5. ilugd

    ilugd Beware of the innocent

    Joined:
    Dec 25, 2005
    Messages:
    1,009
    Likes Received:
    3
    Trophy Points:
    0
  6. OP
    OP
    legolas

    legolas New Member

    Joined:
    Feb 8, 2005
    Messages:
    959
    Likes Received:
    0
    Trophy Points:
    0
    mediator, using %s instead of %c solved the prbs... i ll try out the other options also.. thks to every1 for ur timely help!!! ufff... this thing!!! thks guys!!

    /legolas
     
Thread Status:
Not open for further replies.

Share This Page