0

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;
}
user1681673
  • 368
  • 1
  • 6
  • 28
  • So what do you input and what do you expect `num1`, `num2`, `iochar`, and `operator` to be? – chris Oct 06 '12 at 01:54
  • The user will enter an equation like 2 + 3. 2 will be num1, + will be operator, and 3 will be num2. Iochar just takes all of the input. – user1681673 Oct 06 '12 at 02:01
  • You only store the first character in `iochar`, get rid of a character, and then use that same character to set all variables, since you never call `getchar` to read on. – chris Oct 06 '12 at 02:03
  • Looking back on the output I was getting that makes sense. How do I fix that? – user1681673 Oct 06 '12 at 03:08
  • 1
    You're using a bunch of low level I/O you don't really understand, and your program is quite complex compared to your current programming level. Put this program aside for a while and write really dirt-simple little programs that you do understand, and add features to them as your understanding increases. Experiment and figure out how features of the language work. – NovaDenizen Oct 06 '12 at 04:07

2 Answers2

0

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.

JayC
  • 7,053
  • 2
  • 25
  • 41
0

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.

scaryrawr
  • 777
  • 4
  • 12
  • This seems very helpful. Thank you. I just realized though that I have to account for 2 digit numbers, which I forgot. :/ I'll definitely use your help though. – user1681673 Oct 06 '12 at 03:08
  • Thanks! My getint and putint don't validate the input though, it assumes digits are properly formed, and even though I have a buffer of 255 that's really overkill (since an int can only be like 11 characters long (including the minus sign). up vote =x? – scaryrawr Oct 06 '12 at 03:30
  • I would vote up but I don't have 15 credits. Sorry. I can only use getchar and putchar, not getint, and putint. How do I return ints from the char functions? – user1681673 Oct 06 '12 at 19:33
  • If you look at my post, getint and putint are written using getchar and putchar. There is no getint or putint in C. – scaryrawr Oct 06 '12 at 21:32
  • Can you elaborate on this? I don't quite understand. Thank you. EDIT: Actaully, I just figured it out. I'll try it out. – user1681673 Oct 06 '12 at 23:20
  • Yep, it's implemented in my post. I think your professor is expecting a different kind of implementation though since numbers are only going to be two digits tops (probably something a lot simpler). – scaryrawr Oct 07 '12 at 00:57