Thread: C Calculator With getchar/putchar

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    8

    C Calculator With getchar/putchar

    Hello,

    Beginning C programming and first time C programming lessons ever. The code below is for a C calculator using putchar/getchar. In the code below, what would be the best way to account for space/multiple spaces before input, space/multiple spaces between the digits and operand, no spaces between operand and digits, and spaces after input by the user is complete.

    Right now the code will work with one space between the digits and operand.

    Meaning, 10 + 5 works. However, for example, 10+5 does not and ____5 + 10 (where __ = space), or 10+5______, or 10_________+ 10 do not work.
    Any advice or help on how to account for multiple spaces in between the digits and operand and before after any user input is so very greatly appreciated.

    Thank you so very much for any and all help with the current code. Really do appreciate your help and time in helping me learn this stuff!!



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<math.h>
    
     int add(int input1,char operand,int input2);
     int subtract(int input1,char operand,int input2);
     int mod(int input1,char operand,int input2);
     int multiply(int input1,char operand,int input2);
     int divide(int input1,char operand,int input2);
     char cont(void);
    
     int main()
     {
    
         int answer =0;
         int ch =0;
         int input1 =0;
         char operand =0;
         int input2 =0;
         int function =0;
         char flag;
    
         do{
    
            input1 =0, input2 =0, operand =0;
    
            printf("\nPlease enter a calculation to be made.\n");
    
              while(((ch = getchar())!=' ')&&(ch != EOF)&&(ch !='\n')){
    
                 if(ch =='-'){
                printf("\nError: no negatives allowed.\n");
    
                 }
    
                elseif(!isdigit(ch)){
                    printf("\nError: number not inputted (first number).\n");
                    }
    
            else{
    
                input1 =(input1 *10)+(ch -'0');
            }
        }
    
    
             while(((ch = getchar())!=' ')&&(ch != EOF)&&(ch !='\n')){
    
                switch(ch){
    
                case'+':
                    operand ='+';
                    break;
    
                case'-':
                    operand ='-';
                    break;
    
                case'%':
                    operand ='%';
                    break;
    
                case'*':
                    operand ='*';
                    break;
    
                case'/':
                    operand ='/';
                    break;
    
                default:
                    printf("Error: input is not one of the allowed operands.");
                    break;
    
                }
    
            }
    
        while(((ch = getchar())!=' ')&&(ch !='\n')){
    
            if(ch =='-'){
                printf("\nError: no negatives allowed.\n");
            }
    
            elseif(!isdigit(ch)){
                printf("\nError: number not inputted (second number).\n");
            }
    
            else{
                input2 =(input2 *10)+(ch -'0');
                }
            }
    
            printf("%d", input1);
            putchar(' ');
    
            printf("%c", operand);
            putchar(' ');
    
            printf("%d", input2);
    
            putchar(' ');
            putchar('=');
            putchar(' ');
    
            if(operand =='+'){
            answer = add(input1, operand, input2);
            printf("%d", answer);
        }
        elseif(operand =='-'){
            answer = subtract(input1, operand, input2);
            printf("%d", answer);
        }
        elseif(operand =='%'){
            answer = mod(input1, operand, input2);
            printf("%d", answer);
        }
        elseif(operand =='*'){
            answer = multiply(input1, operand, input2);
            printf("%d", answer);
        }
        elseif(operand =='/'){
            answer = divide(input1, operand, input2);
            printf("%d", answer);
    
        }
    
        flag = cont();
    
        }
    
        while(flag =='y'|| flag =='Y');
    
        return0;
       }
    
    int add(int input1,char operand,int input2){
    
        return input1 + input2;
    
    }
    
    int subtract(int input1,char operand,int input2){
    
        return input1 - input2;
    
    }
    
    int mod(int input1,char operand,int input2){
    
        return input1 % input2;
    
    }
    
    int multiply(int input1,char operand,int input2){
    
        return input1 * input2;
    
    }
    
    int divide(int input1,char operand,int input2){
    
        return input1 / input2;
    
    }
    
    char cont()
    {
    
        char flag;
        printf("\nDo you want to process another calculation (y/n)? ");
        scanf("%c%*c",&flag);
        return flag;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Firstly, what you're calling an "operand" is actually an "operator". Operands are what operators operate on.

    Now to the problem you're having with spaces. You're currently using space as a delimiter. Instead, just ignore spaces and use digits/non-digits as delimiters.
    Code:
        while ((ch = getchar()) != EOF && ch != '\n') {
            if (isspace(ch)) continue;   // ignore spaces
            if (!isdigit(ch)) break;     // end loop when non-digit found
            n = n * 10 + (ch - '0');
        }
    
        // now ch contains the operator (or an invalid character)
        // You can check that it's a valid operator like so:
        if (strchr("+-*%/", ch) == NULL) {
            // error: invalid operator
        }
    
        operator = ch;
    
        // same code as before to read the next number
    There's probably a little more to it than that (e.g., if there's no number before the operator the number-reading loop will end and n will contain 0 with no way to tell that that wasn't what was entered). Still, that's the basic idea.
    Post back if you have any questions!

  3. #3

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    8

    Question Follow up question

    Thank you very much for the advice. I have further modified the code to account for spaces as suggested, plus other parts since I am experimenting with different types of statements as part of the learning process.

    However, another question related to the "break" adviced above by algorism. What would be the best way to break out of the inner while loops and back to the do... while where it asks the user for a yes/no input? With the new code when it breaks out I get:

    Please enter a calculation to be made.-10 + 10
    Error: input is not one of the allowed operands.Error: input is not one of the a
    llowed operands.0 - 0 = 0
    Do you want to process another calculation (y/n)? Press any key to continue . .

    .
    after breaking out of the first while loop or:

    Please enter a calculation to be made.10 + -10
    10 + 0 = 10
    Do you want to process another calculation (y/n)? Press any key to continue . .
    when breaking out of the second while look and the program ends.

    Any suggestions on how to get back into the do...while and wait for user input is greatly appreciated.

    Thank you very much!!!


    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    
    
     int add(int input1, char operand, int input2);
     int subtract(int input1, char operand, int input2);
     int mod(int input1, char operand, int input2);
     int multiply(int input1, char operand, int input2);
     int divide(int input1, char operand, int input2);
     char cont(void);
    
    
     int main()
     {
    
    
         int answer = 0;
         int ch = 0;
         int input1 = 0;
         char operand = 0;
         int input2 = 0;
         int function = 0;
         char flag;
    
    
         do {
    
    
            input1 = 0, input2 = 0, operand = 0;
    
    
            printf("\nPlease enter a calculation to be made.\n");
    
    
              while (((ch = getchar()) != EOF) && (ch != '\n')){
    
    
                  if (ch == ' ') continue;
                  if (!(ch >= '0' && ch <= '9')) break;
    
    
                    input1 = (input1 * 10) + (ch - '0');
     
        }
              operand = ch;
    
    
    
    
             while (((ch = getchar()) != ' ') && (ch != EOF) && (ch != '\n')){
    
    
                switch (ch){
    
    
                case '+':
                    operand = '+';
                    break;
    
    
                case '-':
                    operand = '-';
                    break;
    
    
                case '%':
                    operand = '%';
                    break;
    
    
                case '*':
                    operand = '*';
                    break;
    
    
                case '/':
                    operand = '/';
                    break;
    
    
                default:
                    printf("Error: input is not one of the allowed operands.");
                    break;
    
    
                }
    
    
    
    
            }
    
    
              while (((ch = getchar()) != EOF) && (ch != '\n')){
    
    
                  if (ch == ' ') continue;
                  if (!(ch >= '0' && ch <= '9')) break;
    
    
                        input2 = (input2 * 10) + (ch - '0');
        }
    
    
            printf("%d", input1);
            putchar(' ');
    
    
            printf("%c", operand);
            putchar(' ');
    
    
            printf("%d", input2);
    
    
            putchar(' ');
            putchar('=');
            putchar(' ');
    
    
            if (operand == '+'){
            answer = add(input1, operand, input2);
            printf("%d", answer);
        }
        else if (operand == '-'){
            answer = subtract(input1, operand, input2);
            printf("%d", answer);
        }
        else if (operand == '%'){
            answer = mod(input1, operand, input2);
            printf("%d", answer);
        }
        else if (operand == '*'){
            answer = multiply(input1, operand, input2);
            printf("%d", answer);
        }
        else if (operand == '/'){
            answer = divide(input1, operand, input2);
            printf("%d", answer);
    
    
        }
    
    
        flag = cont();
    
    
        }
    
    
        while (flag == 'y' || flag == 'Y');
    
    
        return 0;
    
    
       }
    
    
    int add(int input1, char operand, int input2){
    
    
        return input1 + input2;
    
    
    }
    
    
    int subtract(int input1, char operand, int input2){
    
    
        return input1 - input2;
    
    
    }
    
    
    int mod(int input1, char operand, int input2){
    
    
        return input1 % input2;
    
    
    }
    
    
    int multiply(int input1, char operand, int input2){
    
    
        return input1 * input2;
    
    
    }
    
    
    int divide(int input1, char operand, int input2){
    
    
        return input1 / input2;
    
    
    }
    
    
    char cont()
    {
    
    
        char flag;
        printf("\nDo you want to process another calculation (y/n)? ");
        scanf("%c%*c", &flag);
        return flag;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() and putchar(c)
    By an96 in forum C Programming
    Replies: 5
    Last Post: 06-13-2015, 04:21 PM
  2. Replies: 4
    Last Post: 03-22-2014, 05:03 PM
  3. Getchar,putchar
    By Filster in forum C Programming
    Replies: 26
    Last Post: 08-03-2011, 04:45 PM
  4. getchar() and putchar()
    By kawaikx15 in forum C Programming
    Replies: 5
    Last Post: 04-13-2011, 11:02 AM
  5. Calculator using getchar, putchar
    By arlen20002000 in forum C Programming
    Replies: 5
    Last Post: 03-26-2008, 10:37 AM

Tags for this Thread