| | |
starting a calculator program in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
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
for somereason the program isnt working and i still am not sure how to add an integer only mode
any help would be nice
basically it needs to have + - / * ^ functions as well as an integer only mode
the code so far
c Syntax (Toggle Plain Text)
#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
There are some things wrong with you program:
This function is made inside of main() which is impossible.
I think you mean switch() ?
I think you mean '+' instead of +
Same thing with the other chars.
you really should use code-indention, this is very hard to read.
regards Niek
•
•
•
•
float do0p(char op, float num1, float num2) {
This function is made inside of main() which is impossible.
•
•
•
•
select (op) {
•
•
•
•
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.
regards Niek
Last edited by niek_e; Feb 2nd, 2007 at 6:59 am.
•
•
•
•
hm i changed the code and it complies now, but when i try to inter a number, an error appears
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jan 2007
Posts: 27
Reputation:
Solved Threads: 0
Well the updated code is now
i type in 4
+
5
and it returns 1245060
is there something wrong with the case portion?
c Syntax (Toggle Plain Text)
#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?
Last edited by cusado; Feb 2nd, 2007 at 3:14 pm.
Nope, the problem is with how you print the result.
&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. 
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. 
Is this the first program you've tried to build in Pelles C? It could be a corrupt installation.
C Syntax (Toggle Plain Text)
printf("%d", &acc);

C Syntax (Toggle Plain Text)
printf( "%f", acc );

Is this the first program you've tried to build in Pelles C? It could be a corrupt installation.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
![]() |
Similar Threads
- Java Swing Calculator program not running. It has 0 errors (Java)
- Wierd error messages with calculator program (C++)
- need help with calculator program in C (C)
Other Threads in the C Forum
- Previous Thread: 15 puzzle problem
- Next Thread: C File Functions
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list lowest matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scheduling segmentationfault send single socketprogramming spoonfeeding stack standard strchr string student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32api windows.h






