Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 28-03-2006, 03:10 PM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default problem with C, pls helppppp with this!!!!!!!!!


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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 28-03-2006, 03:37 PM   #2 (permalink)
Beware of the innocent
 
ilugd's Avatar
 
Join Date: Dec 2005
Posts: 1,024
Default

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
ilugd is offline  
Old 28-03-2006, 03:42 PM   #3 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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
sakumar79 is offline  
Old 28-03-2006, 04:04 PM   #4 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default

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!
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 28-03-2006, 04:19 PM   #5 (permalink)
Beware of the innocent
 
ilugd's Avatar
 
Join Date: Dec 2005
Posts: 1,024
Default

on windows i would use getche, but from this article it seems that linux doesn't support that. (http://www.phim.unibe.ch/comp_doc/c_...S/getchar.html)

and another exactly appropriate article
http://www.phanderson.com/C/scanf.html
ilugd is offline  
Old 28-03-2006, 05:09 PM   #6 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

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
__________________
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila.
legolas is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Tenida
- by clinton
- by gohan89
- by icebags

Advertisement




All times are GMT +5.5. The time now is 08:13 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2