im writing a calculator program in c and having some trouble..
basically it needs to have + - / * ^ functions as well as an integer only mode

the code so far

#include <stdio.h>
#include <ctype.h>
#include <math.h>



int main(int argc, char *argv[]) {
    float acc, num;
    char op;


float do0p(char op, float num1, float num2) {
    select (op) {
        case + : return num1 + num2;
            break;
        case - : return num1 - num2;
            break;
        case * : return num1 * num2;
            break;
        case / : return num1 / num2;
            break;    
        case ^ : return pow(num1,num2);
            break;
        default: return num1;
    }
}



    printf("Welcome to calculator!\n");
    while(1) {
        scanf("%f", acc);
        while(1) {
            do op = getchar(); while(isspace(op));
            if(op == 'q') return 0;
            else if(op == 'c') break;
        scanf("%f", num);
            acc = do0p(op, acc, num);
            printf("%d", acc);
        }
    }






    return 0;




}

for somereason the program isnt working and i still am not sure how to add an integer only mode
any help would be nice

Recommended Answers

All 10 Replies

There are some things wrong with you program:

float do0p(char op, float num1, float num2) {

This function is made inside of main() which is impossible.

select (op) {

I think you mean switch() ?

case +

I think you mean '+' instead of +
Same thing with the other chars.

you really should use code-indention, this is very hard to read.

hm i changed the code and it complies now, but when i try to inter a number, an error appears

scanf() needs an ampersand before the variable name when inputting numbers.

scanf("%f", &num);

hm i changed the code and it complies now, but when i try to inter a number, an error appears

You must give us details. "An error" can be any of 300+ things.

Explain fully
1) what is wrong
2) where the problem is
3) what it should do instead
4) cut and paste errors (don't paraphrase them)

That way our psychic powers don't have to be used. ;)

Well the updated code is now

#include <stdio.h>
#include <ctype.h>
#include <math.h>

float do0p(char op, float acc, float num) {
    switch (op) {
        case '+' : return acc + num;
            break;
        case '-' : return acc - num;
            break;
        case '*' : return acc * num;
            break;
        case '/' : return acc / num;
            break;    
        case '^' : return pow(acc,num);
            break;
        default: return acc;
    }
}


int main(int argc, char *argv[]) {
    float acc, num;
    char op;

    printf("Welcome to calculator!\n");
    while(1) {
        printf("enter first number\n");
        scanf("%f", &acc);
            while(1) {
                do op = getchar(); while(isspace(op));
                if(op == 'q') return 0;
                else if(op == 'c') break;
                printf("enter second number\n");
                scanf("%f", &num);
            acc = do0p(op, acc, num);
            printf("%d", &acc);
        }
    }






    return 0;




}

i type in 4
+
5
and it returns 1245060
is there something wrong with the case portion?

also, in pellesC i keep getting a POLINK: fatal error: Access is denied.
message, but if i put the same code in Dev-C++ it doesnt show up(im programming Dev-C++ in C as well)

is there something wrong with the case portion?

Nope, the problem is with how you print the result.

printf("%d", &acc);

&acc means the address of acc, and %d expects an int. If you change %d to %f and &acc to acc, things begin to work. :)

printf( "%f", acc );

The rules for using scanf() and printf() are frustratingly different, especially when you know everything is right but still keep getting 0 instead of a number you expect. ;)

also, in pellesC i keep getting a POLINK: fatal error: Access is denied.
message, but if i put the same code in Dev-C++ it doesnt show up(im programming Dev-C++ in C as well)

Is this the first program you've tried to build in Pelles C? It could be a corrupt installation.

thanks! that helped out a lot

Any idea on how to add an integer only mode? where it disregards any numbers after decimal

Any idea on how to add an integer only mode? where it disregards any numbers after decimal

There's no such thing as integer only mode with stream input. To do that you would have to restrict the keypresses, and C/C++ can't do that natively. Why not? Because input is buffered for efficiency. By the time you get a character, there could be hundreds more already behind it.

But like all good things you can fake it! :) scanf() is eccentric, but very predictable. When you say %d, it only reads an integer. If it fails to read an integer, you can flush the stream. It's a heavy handed method, but it works.

while ( 1 ) {
  if ( scanf ( "%d", &var ) == 1 ) {
    /* We read an integer, so we're done */
    break;
  }
  else {
    /* scanf() failed, so flush the stream and try again */
    int ch;
    do
      ch = getchar();
    while ( ch != '\n' );
  }
}

The conditions control how and when you flush. You can do it only after scanf() fails, or after every integer is successfully read, or any other time that makes sense. The trick is just to remember that the stream contains junk characters that don't mean anything until your program interprets them. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.