943,907 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5393
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 2nd, 2007
0

starting a calculator program in C

Expand Post »
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
  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <math.h>
  5.  
  6.  
  7.  
  8. int main(int argc, char *argv[]) {
  9. float acc, num;
  10. char op;
  11.  
  12.  
  13. float do0p(char op, float num1, float num2) {
  14. select (op) {
  15. case + : return num1 + num2;
  16. break;
  17. case - : return num1 - num2;
  18. break;
  19. case * : return num1 * num2;
  20. break;
  21. case / : return num1 / num2;
  22. break;
  23. case ^ : return pow(num1,num2);
  24. break;
  25. default: return num1;
  26. }
  27. }
  28.  
  29.  
  30.  
  31. printf("Welcome to calculator!\n");
  32. while(1) {
  33. scanf("%f", acc);
  34. while(1) {
  35. do op = getchar(); while(isspace(op));
  36. if(op == 'q') return 0;
  37. else if(op == 'c') break;
  38. scanf("%f", num);
  39. acc = do0p(op, acc, num);
  40. printf("%d", acc);
  41. }
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. return 0;
  50.  
  51.  
  52.  
  53.  
  54. }

for somereason the program isnt working and i still am not sure how to add an integer only mode
any help would be nice
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 2nd, 2007
0

Re: starting a calculator program in C

There are some things wrong with you program:

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

This function is made inside of main() which is impossible.
Quote ...
select (op) {
I think you mean switch() ?
Quote ...
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.
Last edited by Nick Evan; Mar 31st, 2010 at 9:16 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Feb 2nd, 2007
0

Re: starting a calculator program in C

hm i changed the code and it complies now, but when i try to inter a number, an error appears
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 2nd, 2007
0

Re: starting a calculator program in C

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

scanf("%f", &num);
Last edited by Colin Mac; Feb 2nd, 2007 at 10:17 am.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Feb 2nd, 2007
0

Re: starting a calculator program in C

Click to Expand / Collapse  Quote originally posted by cusado ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,729 posts
since May 2006
Feb 2nd, 2007
0

Re: starting a calculator program in C

Well the updated code is now
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <math.h>
  4.  
  5. float do0p(char op, float acc, float num) {
  6. switch (op) {
  7. case '+' : return acc + num;
  8. break;
  9. case '-' : return acc - num;
  10. break;
  11. case '*' : return acc * num;
  12. break;
  13. case '/' : return acc / num;
  14. break;
  15. case '^' : return pow(acc,num);
  16. break;
  17. default: return acc;
  18. }
  19. }
  20.  
  21.  
  22. int main(int argc, char *argv[]) {
  23. float acc, num;
  24. char op;
  25.  
  26. printf("Welcome to calculator!\n");
  27. while(1) {
  28. printf("enter first number\n");
  29. scanf("%f", &acc);
  30. while(1) {
  31. do op = getchar(); while(isspace(op));
  32. if(op == 'q') return 0;
  33. else if(op == 'c') break;
  34. printf("enter second number\n");
  35. scanf("%f", &num);
  36. acc = do0p(op, acc, num);
  37. printf("%d", &acc);
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. return 0;
  47.  
  48.  
  49.  
  50.  
  51. }


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.
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 2nd, 2007
0

Re: starting a calculator program in C

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)
Last edited by cusado; Feb 2nd, 2007 at 5:11 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 2nd, 2007
0

Re: starting a calculator program in C

Click to Expand / Collapse  Quote originally posted by cusado ...
is there something wrong with the case portion?
Nope, the problem is with how you print the result.
  1. 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.
  1. 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.
Click to Expand / Collapse  Quote originally posted by cusado ...
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.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Feb 2nd, 2007
0

Re: starting a calculator program in C

thanks! that helped out a lot
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007
Feb 3rd, 2007
0

Re: starting a calculator program in C

Any idea on how to add an integer only mode? where it disregards any numbers after decimal
Reputation Points: 10
Solved Threads: 0
Light Poster
cusado is offline Offline
27 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: 15 puzzle problem
Next Thread in C Forum Timeline: C File Functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC