![]() |
| | #1 |
| Registered User Join Date: Oct 2008
Posts: 76
| scanf interactive program 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);
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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,816
| 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 | |
| | #3 |
| Registered User Join Date: Oct 2008
Posts: 76
| 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); }
}
}
|
| rocketman03 is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,816
| 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 | |
| | #5 |
| Registered User Join Date: Oct 2008
Posts: 76
| 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 | |
| | #6 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,816
| Quote:
| |
| tabstop is offline | |
| | #7 |
| Registered User Join Date: Oct 2008
Posts: 76
| 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 | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,816
| |
| tabstop is offline | |
| | #9 |
| Registered User Join Date: Oct 2008
Posts: 76
| 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();
}
}
|
| rocketman03 is offline | |
| | #10 |
| Registered User Join Date: Oct 2008
Posts: 76
| oh my god! nevermind , i put commas in the scanf !..my fault!..thank you sooo much! it worked!! Thank you !! |
| rocketman03 is offline | |
![]() |
| Tags |
| array, interactive, scanf |
| Thread Tools | |
| Display Modes | |
|
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 |