problem in using ungetch( )

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cworld
    New Member
    • Jan 2007
    • 15

    problem in using ungetch( )

    Hi all
    I am making an RPN calculator in which i want to read data from input. so i used getchar( ) and able to read character by character. But when the while condition fullfill then it comes out from the loop but retain the next character read from input so i want to ungetch to the input. but it does not work. Plz help me, my code is:
    Code:
    /******** Program for Read_Input Function**********/
    
    # include <stdio.h>
    # include <ctype.h>
    # include <conio.h>
    # include "rpn.h"
    char in[10];
    char c;
    
    read_input(char in[10])
    
    	{
    	
    	int i;
    		c=getchar();
    			in[0]=c;
    			in[1]='\0';
    		
    		if (c==' '||c=='\t')
    							
    		while ((c=getchar())==' '||c=='\t');
    							
    		if(isdigit(c))
    		{
    		i=1;
    		
    		while (isdigit(c=getchar()))
    			in[i++]=c;
    			in[i]='\0';
    		if (c!=EOF)
    			printf("Value of c before Ungetch= %d\n",c);
    			ungetch(c);
    		
    		return NUMBER;
    			}
    
    		if(isalpha(c))
    			
    			return ERROR_ALPHA;
    return c;
    	}
    /****************************************/
    This function is called in the main program but again when i call this function it gets the next charater from the input. should i make a userdifine function for getch and ungetch like as given in Ritchie book chapter4. or some otherthings??

    thankyou
    cworld
    Last edited by Ganon11; Apr 5 '07, 02:22 PM. Reason: code tags added
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by cworld
    Hi all
    I am making an RPN calculator in which i want to read data from input. so i used getchar( ) and able to read character by character. But when the while condition fullfill then it comes out from the loop but retain the next character read from input so i want to ungetch to the input. but it does not work.
    The ungetch() function is part of the conio stuff which doesn't use the streams
    used by the stdio library. You should use the ungetc() function instead; it will
    put back the last character read in the input stream.

    kind regards,

    Jos

    Comment

    • cworld
      New Member
      • Jan 2007
      • 15

      #3
      Originally posted by JosAH
      The ungetch() function is part of the conio stuff which doesn't use the streams
      used by the stdio library. You should use the ungetc() function instead; it will
      put back the last character read in the input stream.

      kind regards,

      Jos
      Thankyou Jo for your quick response.
      but still i m getting error:
      *************** *************** *************** ******
      error C2660: 'ungetc' : function does not take 1 parameters stack.cpp
      *************** *************** *************** ******
      Actually i am taking input from keyboard so it should return character to the keyboard, but it does not work.

      any idea?

      cworld

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by cworld
        but still i m getting error:
        *************** *************** *************** ******
        error C2660: 'ungetc' : function does not take 1 parameters stack.cpp
        *************** *************** *************** ******
        Actually i am taking input from keyboard so it should return character to the keyboard, but it does not work.

        any idea?
        Yep, I have an idea: the ungetc() function takes one parameter which is the
        character to put back in the stream, exactly as your compiler's diagnostic
        message tried to tell you.

        kind regards,

        Jos

        Comment

        Working...