Output coming out as an upside down question mark and prompt is coming out twice in calculator program in C

1.3k Views Asked by At

I'm writing a simple calculation program and I can't get any valid output. All I'm getting is an upside down question mark. Also, I have a prompt at the end of the program to ask the user if they would like to enter in another calculation. However, when I enter a calculation the prompt comes up twice in the console. Does anyone know why these things are happening? Lastly, I can only use getchar and putchar to handle the input and output. Thanks in advance for the help.

int addFunction( int, int);
int subtractFunction(int, int);
int multiplyFunction(int, int);
int modulusFunction(int, int);
float divideFunction(float, float);

int main(int argc, const char * argv[])
{

int num1 = 0, num2 = 0, result = 0;
char  continuePrompt,  iochar = 0, operator = 0;

do {
    iochar = getchar();
    getchar();


    if ((iochar >= 0) && (iochar <= 20000)) {
        num1 = iochar; 
    }

    if ((iochar == '+') || (iochar == '-') || (iochar == '*') || (iochar == '/') || (iochar == '%')) {
        operator = iochar; 
    }

    if ((num1 >= 0) || ((iochar >= 0) && (iochar <= 20000))){
        num2 = iochar;
    }

    switch (operator) {

        case '+':
           iochar  = addFunction(num1, num2);
            break;

        case '-':
            iochar = subtractFunction(num1, num2);
            break;

        case '*':
            iochar = multiplyFunction(num1, num2);
            break;

        case '%':
            iochar = modulusFunction(num1, num2);
            break;

        case '/':
            iochar = divideFunction(num1, num2);
            break;

    }

    putchar(iochar);



    printf("Would you like to make another calulation? (y or n)");
    scanf("%c", &continuePrompt);

} while (continuePrompt != 'n');
return 0;
}

int addFunction(int x, int y){
    return x + y;
}

int subtractFunction(int x, int y){
     return x - y;
}

int multiplyFunction(int x, int y){
    return x * y;
}

int modulusFunction(int x, int y){
    return x % y;
}

float divideFunction(float x, float y){
    return x / y;
}
2

There are 2 best solutions below

0
On

The function getchar and gets a character from the console and putchar puts a character to the console. The are not general input/output functions. Your code reads like you expect getchar to read in decimal representations of integers and putchar to print decimal representation of integers, but they don't work that way. Since you can only use getchar and putchar, you are going to have to write your own input/output methods with them to parse your inputs and display them correctly. So, first, figure out how to parse and output integers. Then use those methods where you're expecting to read or write integers (and floats if you have to display the estimated values for division). It may help to have a helper method that actually grabs the "current" numerical string from wherever you are in the expression.

6
On

Some basics...

A character value '0' does not equal the integer value 0 on in ascii it has the integer value 48. If you're only having each number be 1 digit, it would be something like:

char c = getchar();
// Assuming the user only will input a number...
int number = c - '0';

For reading in an integer with just getchar() I would do something like:

#include <stdio.h>
#include <math.h> // for pow

int getint()
{
    char c;
    char buffer[255]; // way larger than an integer will ever be I think...
    int numlen = 0;
    int number = 0;
    int x;
    int multfornegative = 1;

    while ((c = getchar()) != '\n') {
        buffer[numlen++] = c;
    }

    for (x = numlen - 1; x >= 0; x--) {
        c = buffer[(numlen - 1) - x];
        if (c == '-') {
            multfornegative *= -1;
        } else {
            number += (c - '0') * (int)pow(10, x);
        }
    }

    return number * multfornegative;
 }

for output you would do something like...

void putint(int number)
{
    char digit;
    int x;
    int start;

    if (number < 0) {
        putchar('-');
        number *= -1;
    }

    start = log(number) / log(10);

    for (x = start; x >= 0; x--) {
        digit = ((number / (int)pow(10, x)) % 10) + '0';
        putchar(digit);
    }
}

Also, try to break apart your input, the way you have it in the loop ends up confusing which is messing up your logic.

int num1;
char op;
int num2;
int ans;

do {
    num1 = getint();
    op = getchar(); getchar();
    num2 = getint();

    switch(op) {
        case '+': ans = num1 + num2; break;
        case '-': ans = num1 - num2; break;
        // And so on...
    }
    putint(ans);
while (1);

The C Programming Language is an amazing book to read for learning C, written by the inventors of C themselves.