Like Tree1Likes
  • 1 Post By quzah

Arithmetic calculation using getchar and putchar

This is a discussion on Arithmetic calculation using getchar and putchar within the C Programming forums, part of the General Programming Boards category; Hello. I am new to programming in C an found this forum. I need some help with a program that ...

  1. #1
    Registered User
    Join Date
    Apr 2011

    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:
    Please enter an equation for evaluation
    at this point the output will read
    Would you like to continue? Y or N: y
    The program will close.

    My code (with global variable) is as follows:
    #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;
                 printf("Please enter an equation for evaluation.\n");
    			if((ch==' ')||(ch=='\t')){
    			else if(!isdigit(ch)){
    			else if(isdigit(ch)){
    			else if(!isdigit(ch)&&(ch=='\n')){				
    //				ch=getchar();
    //				putchar(ch);
    			else if(!isdigit(ch)&&(ch!=EOF)){
    			else if(Oper=='-')
    			else if(Oper=='*')
    			else if(Oper=='/')
    			else if(Oper=='%')
    			printf("%d%c%d=%d\n",Num1, Oper, Num2, Result);
    			printf("Would you like to continue? Y or N:");
    			scanf ("%c%*c", &answer);
    				printf("Thanks. Goodbye.");
    	return 0;
    int Convert(char c){
    	int ch;
    	int sum=c-'0';
    //	char Operand='/0';
    	while(((ch=getchar())!=' ')&&(ch!='\n')){
    			if(ch==' '||ch=='/t')
    	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 04:07 PM. Reason: added code tags

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.
    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.

    Last edited by quzah; 04-03-2011 at 04:35 PM.
    cray82 likes this.
    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, 03:57 PM
  3. Calculator using getchar, putchar
    By arlen20002000 in forum C Programming
    Replies: 5
    Last Post: 03-26-2008, 11: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21