starting a calculator program in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

starting a calculator program in C

 
0
  #1
Feb 2nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,968
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: starting a calculator program in C

 
0
  #2
Feb 2nd, 2007
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.

regards Niek
Last edited by niek_e; Feb 2nd, 2007 at 6:59 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: starting a calculator program in C

 
0
  #3
Feb 2nd, 2007
hm i changed the code and it complies now, but when i try to inter a number, an error appears
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: starting a calculator program in C

 
0
  #4
Feb 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: starting a calculator program in C

 
0
  #5
Feb 2nd, 2007
Originally Posted by cusado View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: starting a calculator program in C

 
0
  #6
Feb 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: starting a calculator program in C

 
0
  #7
Feb 2nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: starting a calculator program in C

 
0
  #8
Feb 2nd, 2007
Originally Posted by cusado View Post
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.
Originally Posted by cusado View Post
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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: starting a calculator program in C

 
0
  #9
Feb 2nd, 2007
thanks! that helped out a lot
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 27
Reputation: cusado is an unknown quantity at this point 
Solved Threads: 0
cusado cusado is offline Offline
Light Poster

Re: starting a calculator program in C

 
0
  #10
Feb 3rd, 2007
Any idea on how to add an integer only mode? where it disregards any numbers after decimal
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC