Thread: Help Wanted for Simple C Arithmetic Calculator Involving getchar()

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Help Wanted for Simple C Arithmetic Calculator Involving getchar()

    I've been stuck at a roadblock for almost an hour now. I'm trying to write a program that takes user input from the command prompt of the form:
    [n # spaces] [operand] [n # spaces] [operator] [operand] [n#spaces]

    Where each [n # spaces] component means some unknown number of spaces. For example, the user should be able to enter some expression such as: " 380 * 4 " and get both their output echoed and the result of the expression. We can only use getchar() to read input; no string handling functions are allowed. We can also assume the operands are positive integers. For now I believe these are the only relevant conditions of the problem necessary to share.

    Initially I thought the program would be straightforward and to some extent it was; I got the echo to work up to and including the operator as well as the answer function to work, but after a few small changes I was foolish enough to not keep track of the program seems to hardly work at all. I had to quickly teach myself pointers because I wanted to circumvent the supposed fact that C cannot pass parameters to functions by reference like you can in C++, so I'm betting the problem lies there, but again all of my functions seem to have been working just a while ago.

    Source Code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    
    //Prototypes
    int SpaceCounts(int* iovar_p);
    int Operand(int* iovar_p);
    char Operator(int* iovar_p);
    double answer(int operand1, int operand2, char Arsign);
    void PrintSpaces(int spaces);
    
    
    int main()
    {
        int iovar = getchar();
        int spaces1, spaces2, spaces3, spaces4;
        char ArSign;
        int operand1, operand2;
    
    
        spaces1 = SpaceCounts(&iovar);
        printf("%d", spaces1);
        operand1 = Operand(&iovar);
        spaces2 = SpaceCounts(&iovar);
        ArSign = Operator(&iovar);
        spaces3 = SpaceCounts(&iovar);
        operand2 = Operand(&iovar);
        spaces4 = SpaceCounts(&iovar);
    
    
        //Output
        PrintSpaces(spaces1);
        printf("%d", operand1);
        PrintSpaces(spaces2);
        printf("%c", ArSign);
        PrintSpaces(spaces3);
        printf("%d", operand2);
        PrintSpaces(spaces4);
        printf("= ");
        printf("%6.2f", answer(operand1, operand2, ArSign));
    
    
    
    
    
    
    
    
        system("pause");
        return 0;
    }
    
    
    
    
    int SpaceCounts(int* iovar_p)
    {
        *iovar_p;
        int spaces = 0;
        while (*iovar_p == ' ');
        {
            spaces++;
            *iovar_p = getchar();
        }
    
    
        return spaces;
    }
    
    
    //Converts characters to integers; using code from textbook here
    int Operand(int* iovar_p)
    {
        int operand = 0;
    
    
        while (isdigit(*iovar_p))
        {
            operand = operand * 10 + (*iovar_p - '0');
            *iovar_p = getchar();
        }
        return (operand);
    }
    
    
    //Reads in arithmetic operator
    char Operator(int* iovar_p)
    {
        *iovar_p;
        char ch = (char)*iovar_p;
        switch (ch)
        {
        case '+':
            return ch;
            *iovar_p = getchar();
            break;
        case '-':
            return ch;
            *iovar_p = getchar();
    
    
            break;
        case '*':
            return ch;
            *iovar_p = getchar();
    
    
            break;
        case '/':
            return ch;
            *iovar_p = getchar();
    
    
            break;
        case '%':
            return ch;
            *iovar_p = getchar();
    
    
            break;
        default:
            exit(1);
        }
    
    
    
    
    
    
    }
    
    
    //Calculator Function
    double answer(int operand1, int operand2, char Arsign)
    {
        if (Arsign = '+')
            return (operand1 + operand2);
        else if (Arsign = '-')
            return (operand1 - operand2);
        else if (Arsign = '*')
            return (operand1 * operand2);
        else if (Arsign = '/')
            return ((double)operand1 / operand2);
        else
            return (operand1 % operand2);
    }
    
    
    void PrintSpaces(int spaces)
    {
        int i;
        for (i = 0; i < spaces; i++)
            printf(" ");
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Where each [n # spaces] component means some unknown number of spaces.
    Does n include zero ?

    > [n # spaces] [operand] [n # spaces] [operator] [operand] [n#spaces]
    Should there be [n#spaces] between operator and the second operand?

    > //Output
    > PrintSpaces(spaces1);
    > ...
    Are you really meant to output the input exactly, spaces and all?

    BUGS
    > while (*iovar_p == ' ');
    The ; at the end of this line means the code will lock up in an infinite loop if the condition is initially true.

    > if (Arsign = '+')
    You mean == here (and in several other places where you made the same mistake).
    Code:
    $ gcc -Wall main.c
    main.c: In function ‘SpaceCounts’:
    main.c:59:5: warning: statement with no effect [-Wunused-value]
         *iovar_p;
         ^
    main.c: In function ‘Operator’:
    main.c:90:5: warning: statement with no effect [-Wunused-value]
         *iovar_p;
         ^
    main.c: In function ‘answer’:
    main.c:137:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         if (Arsign = '+')
         ^
    main.c:139:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         else if (Arsign = '-')
         ^
    main.c:141:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         else if (Arsign = '*')
         ^
    main.c:143:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
         else if (Arsign = '/')
         ^
    > return ch;
    > *iovar_p = getchar();
    The return happens - and that's it.
    You want to reverse the order of these two statements (again, copy/pasted bug in several places).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failing to Use Recursion - Arithmetic Calculator
    By kapkong in forum C Programming
    Replies: 4
    Last Post: 02-17-2017, 05:37 PM
  2. C Calculator With getchar/putchar
    By st4evr in forum C Programming
    Replies: 3
    Last Post: 03-18-2016, 01:57 AM
  3. Getchar() arithmetic calculator problem.
    By Cowmoogun in forum C Programming
    Replies: 2
    Last Post: 03-28-2012, 04:05 AM
  4. Arithmetic sum calculator
    By fatsrir in forum C Programming
    Replies: 11
    Last Post: 06-13-2011, 01:08 PM
  5. Arithmetic calculation using getchar and putchar
    By cray82 in forum C Programming
    Replies: 1
    Last Post: 04-03-2011, 03:28 PM

Tags for this Thread