Thread: scanf interactive program

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    84

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    Code:
    void menu()
    {
    	int c;
    	while(ask != 'q'){
    	rewind(stdin);
    	printf("p: point , r: rectangle , d: draw , q: quit\n");
    	if(c=scanf("&#37;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!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    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("&#37;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();
    	}
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    %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).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    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 ?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    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("&#37;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 ?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    84
    oh my god! nevermind , i put commas in the scanf !..my fault!..thank you sooo much! it worked!! Thank you !!

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread