Thread: Arithmetic calculation using getchar and putchar

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Unhappy Arithmetic calculation using getchar and putchar

    Hello. I am new to programming in C an found this forum. I need some help with a program that receives user input, does a mathematical calculation and outputs the result. The program consists of rules such as no use of string handling functions, no arrays or no global variables.
    For example, the user enters:
    9+8, the output should be 9+8=17
    9 + 9, the output should be 9+9=18
    *9, output should be error
    2 1/3, output should be an error because there should not be a space between 2 and 1
    6 % 3, output should be 6%3=0
    9 -, output should be an error.
    I'm having a few problems, but the major ones are:
    1) I am using a global variable for the operator value. I have tried creating a function to handle this and I am receiving a '0' value for the operator, thus receiving a result of '0' when I try to implement the function.
    2) The program doesn't evaluate and expression like:
    9 - 9
    Entering this expression will output an Error message when it shouldn't.
    3) After entering an expression such as:
    *9
    Error.
    Please enter an equation for evaluation
    3+8
    at this point the output will read
    9+3=12
    Would you like to continue? Y or N: y
    The program will close.

    My code (with global variable) is as follows:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    
    int Convert(char);
    char Oper = '/0';
    int Operator(char);
    
    int main(void){
            int Num1=0;
            int Num2=0;
            char answer='y';
    //     char Oper='/0';
            char ch;
            int Result=0;
    
            do{
                 printf("Please enter an equation for evaluation.\n");
    
                 while(((ch=getchar())!=EOF)&&(ch!='\n')){
    			if((ch==' ')||(ch=='\t')){
    				ch='/0';
    				continue;
    			}
    			else if(!isdigit(ch)){
    				ch=0;
    				printf("Error.\n");
    				break;
    			}
    
    			else if(isdigit(ch)){
    				Num1=Convert(ch);
    			}
    
    			ch=getchar();
    
    			if(isdigit(ch)){
    				Num2=Convert(ch);
    			}
    			else if(!isdigit(ch)&&(ch=='\n')){				
    				ch=0;
    				printf("Error4.\n");
    				break;
    //				ch=getchar();
    //				putchar(ch);
    			}
    
    			else if(!isdigit(ch)&&(ch!=EOF)){
    				continue;
    			}
    			
    			if(Oper=='+')
    				Result=Num1+Num2;
    			else if(Oper=='-')
    				Result=Num1-Num2;
    			else if(Oper=='*')
    				Result=Num1*Num2;
    			else if(Oper=='/')
    				Result=Num1/Num2;
    			else if(Oper=='%')
    				Result=Num1%Num2;
    			printf("%d%c%d=%d\n",Num1, Oper, Num2, Result);
    			printf("Would you like to continue? Y or N:");
    			scanf ("%c%*c", &answer);
    			if(answer=='n'||answer=='N'){
    				printf("Thanks. Goodbye.");
    				exit(1);
    			}
    		}
    	}while((answer=='y')||(answer=='Y'));
    	return 0;
    }
     
    int Convert(char c){
    	int ch;
    	int sum=c-'0';
    //	char Operand='/0';
    	while(((ch=getchar())!=' ')&&(ch!='\n')){
    		if(!isdigit(ch)){
    			Oper=ch;
    			if(ch==' '||ch=='/t')
    				ch='/0';
    			break;
    		}
    		sum=sum*10+(ch-'0');
    			//printf("Error9.");
    	}
    	return sum;
    }
    //int Operator(char c){
    //	char Oper='/0';
    //	if(c=='+')
    //		Oper='+';
    //	else if(c=='-')
    //		Oper='-';
    //	else if(c=='*')
    //		Oper='*';
    //	else if(c=='/')
    //		Oper='/';
    //	else if(c=='%')
    //		Oper='%';
    //	return Oper;
    //	
    //}
    If someone can help please.
    Last edited by cray82; 04-03-2011 at 03:07 PM. Reason: added code tags

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Go read the thing on the forum that said << !! Posting Code? Read this First !! >>
    2. Come back to your first post and press Edit.
    3. Find the start of your code and right before it, add: [code]
    4. Find the end of your code and right after it, add: [/code]
    5. Press save.

    (What's wrong with the forum lately? Did they change software versions or what? Totally broken lately.)


    You have four states:

    1. Reading the first number.
    2. Reading the operator.
    3. Reading the second number.
    4. Doing the math.

    You can use the same piece of code to read both the first and the second digit. Because all you are doing is reading a number. You might want to look into ungetc so you can put back (note: you can only put back one thing. You can't read 2 characters then put back 2. If you read 2, you can only put back the last one.) one once you find the number has ended.
    Code:
    num = 0
    while char read is digit
        num = (num * 10) + digit;
    /* now that the loop has ended, you have just read something not a digit */
    ungetc( char that you just read )
    return num
    There's you reading a digit. Now you need to read an operator. That should be easy enough. Put those three pieces together and you should be able to do the math.


    Quzah
    Last edited by quzah; 04-03-2011 at 03:35 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  2. getchar putchar: a word on each line
    By krazymanrebirth in forum C Programming
    Replies: 27
    Last Post: 09-29-2009, 02:57 PM
  3. Calculator using getchar, putchar
    By arlen20002000 in forum C Programming
    Replies: 5
    Last Post: 03-26-2008, 10:37 AM
  4. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM
  5. Replies: 2
    Last Post: 01-10-2002, 07:42 PM

Tags for this Thread