C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-14-2008, 08:30 PM   #1
Registered User
 
Join Date: Oct 2008
Posts: 72
scanf interactive program

hey..is there a way to execute scanf before all values have been entered. i am trying to create a program so that when the user types in a 1 2.. it takes him to the 1 , 2 element of a 2d array.
but there is also a function r 1 2 3 4 which takes four parameters n draws an ascii rectangle. so how do i make it so that scanf is able to distinguish between 2 and 4 inputs and knows which function to call;

Code:
scanf("%c %d %d %d %d",&ask,&one,&two,&three,&four);
first one is either a or r and the rest are parameters..i need to make it so that the user can type in a command at any time and the correct function will be called.
theres also d and q.

so the available options are :
a 1 2
r 1 2 3 4
d
q

is it possible with scanf ? or should i use something else
however , i am not allowed to use command line arguments.
rocketman03 is offline   Reply With Quote
Old 11-14-2008, 08:47 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
scanf returns the number of input variables assigned to.

However! There's no need to get all fancy (unless you want to), for scanf will leave unused input in the input buffer. So you can read one character, and then based on that character, read 0, 2 or 4 more numbers during the processing later on, and they will still be there.
tabstop is offline   Reply With Quote
Old 11-14-2008, 08:56 PM   #3
Registered User
 
Join Date: Oct 2008
Posts: 72
Code:
void menu()
{
	int c;
	while(ask != 'q'){
	rewind(stdin);
	printf("p: point , r: rectangle , d: draw , q: quit\n");
	if(c=scanf("%c%d%d%d%d",&ask,&one,&two,&three,&four) < 2)
	{ d(); }
	if ( c < 4)
	{ p(one , two); }
	if (c < 6)
	{ r(one , two , three , four); }
}
}
i did this but even if i press enter it doesnt do anything untill i give it 4 inputs!
rocketman03 is offline   Reply With Quote
Old 11-14-2008, 08:58 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
That's true. I was forgetting that since whitespace is ignored by scanf, more or less, that enter would also be ignored.

So, just do it the way I recommended.
tabstop is offline   Reply With Quote
Old 11-14-2008, 09:19 PM   #5
Registered User
 
Join Date: Oct 2008
Posts: 72
how do i do that ?
i tried this but still have the same problem ?

Code:
void menu()
{
	while(ask != 'q'){
	rewind(stdin);
	printf("p: point , r: rectangle , d: draw , q: quit\n");
	scanf("%c%d%d%d%d",&ask,&one,&two,&three,&four);
	if(ask == 'p')
	p(one , two);
	else if (ask == 'r')
	r(one , two , three , four);
	else if(ask == 'd')
	d();
	else
	q();
	}
}
rocketman03 is offline   Reply With Quote
Old 11-14-2008, 09:34 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
%c%d%d%d%d"
Looks like five things to me. Read in one thing, and one thing only. Then, only if circumstances warrant, do you read in the other four (or two).
tabstop is offline   Reply With Quote
Old 11-14-2008, 09:40 PM   #7
Registered User
 
Join Date: Oct 2008
Posts: 72
Oh ! now i know what you mean..but the thing is were supposed to press enter only once and its supposed to figure out which function to call..if i read only one thing then i will have to press multiple enters right ?
rocketman03 is offline   Reply With Quote
Old 11-14-2008, 09:44 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by rocketman03 View Post
Oh ! now i know what you mean..but the thing is were supposed to press enter only once and its supposed to figure out which function to call..if i read only one thing then i will have to press multiple enters right ?
Of course not.
tabstop is offline   Reply With Quote
Old 11-14-2008, 09:52 PM   #9
Registered User
 
Join Date: Oct 2008
Posts: 72
wow! i didnt know that!..ok..im really close
Code:
void menu()
{
	while(ask != 'q'){
	rewind(stdin);
	printf("p: point , r: rectangle , d: draw , q: quit\n");
	scanf("%c",&ask);
	if(ask == 'p'){
	scanf("%d,%d",&one , &two);
	p(one , two);}
	else if (ask == 'r')
	{
	scanf("%d,%d,%d,%d",&one , &two , &three , &four);
	r(one , two , three , four);
	}
	else if(ask == 'd')
	d();
	else
	q();
	}
}
i have this but if i do this it gives me just the blank board without any points or values..and if i remove the rewind(stdin) it gives me 4 outputs but even those are wrong.. what am i doing wrong ?
rocketman03 is offline   Reply With Quote
Old 11-14-2008, 09:54 PM   #10
Registered User
 
Join Date: Oct 2008
Posts: 72
oh my god! nevermind , i put commas in the scanf !..my fault!..thank you sooo much! it worked!! Thank you !!
rocketman03 is offline   Reply With Quote
Reply

Tags
array, interactive, scanf

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOKKEEPING PROGRAM, need help! yabud C Programming 3 11-16-2006 11:17 PM
Can someome help me with a program please? WinterInChicago C++ Programming 3 09-21-2006 10:58 PM
Need help with my program... Noah C Programming 2 03-11-2006 07:49 PM
my server program auto shut down hanhao Networking/Device Communication 1 03-13-2004 10:49 PM
My program, anyhelp @licomb C Programming 14 08-14-2001 10:04 PM


All times are GMT -6. The time now is 06:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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