954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

starting a calculator program in C

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

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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

scanf("%f", &num);

Colin Mac
Posting Whiz
327 posts since Sep 2006
Reputation Points: 78
Solved Threads: 22
 
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. ;)

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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?

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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)

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
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.

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

thanks! that helped out a lot

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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

cusado
Light Poster
27 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
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. :)

Ravalon
Posting Whiz in Training
213 posts since Dec 2006
Reputation Points: 84
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You