Thread: Simple in-efficient calculator help!

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Exclamation Simple in-efficient calculator help!

    If tested with input 12+12 it only prints the first integer (input1)

    I want it to space it out and do the simple math like:

    Input: 12+12
    Output: 12 + 12 = 24

    I know the math isnt there yet, im trying to get the getchar/putchar to work first. Also the class can only use getchar/putchar, so no string functions like isdigit or anything.
    Thanks again guys!

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
    	int input1;
    	int oper;
    	int input2;
    	int numflag=0;
    	int signflag=0;
    	int end = 0;
    
    	/**Number 1**/
    	while ((input1 = getchar() ) && end!=1)
    	{
    		if ((input1 >= '0') && (input1 <= '9'))
    		{
    			putchar(input1);
    			numflag=1;
    		}
    		else if (numflag==1)
    		{
    			putchar(' ');
    			numflag=0;
    			end=1;
    		}
    	}
    	/**Operator**/
    	while ((oper = getchar() ) && end!=1)
    	{
    		if ((input1 == '+') || (input1 == '-') || (input1 == '*')
    				|| (input1 == '/') || (input1 == '%'))
    		{
    			putchar(oper);
    			numflag=1;
    		}
    		else if (numflag==1)
    		{
    			putchar(' ');
    			numflag=0;
    			end=1;
    		}
    	}
    	/**Number 2**/
    
    	while ((input2 = getchar() ) && end!=1)
    	{
    		if ((input2 >= '0') && (input2 <= '9'))
    		{
    			putchar(input2);
    			numflag=1;
    		}
    		else if (numflag==1)
    		{
    			putchar(' ');
    			numflag=0;
    			end=1;
    		}
    	}
    	
    return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No reason to read the operator separately; it's already sitting in input1 -- it's what causes the loop to break.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM