Currency Converer (Need help with this)

Thread Solved

Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Currency Converer (Need help with this)

 
-1
  #11
Jul 1st, 2009
A few more improvements to the fixed code.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* constants should be centralized */
  5. #define PESO 13.170025
  6. #define USD 0.07593
  7.  
  8. void clear(FILE *strm)
  9. {
  10. int c;
  11.  
  12. do c = getchar(); while (c != '\n' && c != EOF);
  13. }
  14.  
  15. int main()
  16. {
  17. /* double is usually a better choice than float */
  18. double amount;
  19. int choice;
  20.  
  21. /* long strings can be broken down to avoid multiple printfs */
  22. /* if you don't print '\n', flush the stream */
  23. printf("1) USD to PESO\n"
  24. "2) PESO to USD\n"
  25. "> ");
  26. fflush(stdout);
  27.  
  28. /* check input for failure */
  29. if (scanf("%d", &choice) != 1)
  30. {
  31. fputs("Bad input\n", stderr);
  32. return EXIT_FAILURE;
  33. }
  34.  
  35. /* consolidate code to avoid redundancy */
  36. /* if you don't print '\n', flush the stream */
  37. printf("Enter the amount: ");
  38. fflush(stdout);
  39.  
  40. /* check input for failure */
  41. if (scanf("%lf", &amount) != 1)
  42. {
  43. fputs("Bad input\n", stderr);
  44. return EXIT_FAILURE;
  45. }
  46.  
  47. if (choice == 1)
  48. {
  49. printf("%gUSD == %gPESO\n", amount, amount * PESO);
  50. }
  51. else
  52. {
  53. printf("%gPESO == %gUSD\n", amount, amount * USD);
  54. }
  55.  
  56. clear(stdin);
  57. fputs("Press <Enter> to continue...", stderr);
  58. getchar();
  59.  
  60. return EXIT_SUCCESS;
  61. }
Also there is no need for the %.2f thing, %f will do.
The %.2f thing won't work for scanf, it's only for formatting output in printf. For printf, currency converters usually stop at the 0s for precision, so %g is a better choice than %f. %f will print the maximum precision all of the time, even if it's all 0s.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,576
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 848
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Currency Converer (Need help with this)

 
1
  #12
Jul 1st, 2009
I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Currency Converer (Need help with this)

 
0
  #13
Jul 1st, 2009
Originally Posted by Salem View Post
I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all
If the code is already there, you might as well help make it better and hope the guy learns something instead of sitting on your **** waiting for him to fail at life.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 796
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 137
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Currency Converer (Need help with this)

 
0
  #14
Jul 1st, 2009
>If the code is already there, you might as well help make it better and hope the
>guy learns something instead of sitting on your **** waiting for him to fail at life.

Why not nicely pack in a parcel with automatic installers and send him on his postal address with a Project website built?

Not blaming you actually, but it was the post before yours which was the culprit.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6,576
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 848
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Currency Converer (Need help with this)

 
0
  #15
Jul 1st, 2009
No, you blurted it out in response to Hiroshe's first suggestion without waiting for the OP to ask more questions.

If they're still confused, then your "advanced" answer is high and dry in the land of "huh?, wtf"
But hey, it's your time not mine - so that works for me.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 135
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Currency Converer (Need help with this)

 
-1
  #16
Jul 1st, 2009
No, you blurted it out in response to Hiroshe's first suggestion without waiting for the OP to ask more questions.
My post was for both of them, and anyone else reading the thread. I've learned from people who went the extra mile before and now I'm sharing the wealth.

Why not nicely pack in a parcel with automatic installers and send him on his postal address with a Project website built?
If they're still confused, then your "advanced" answer is high and dry in the land of "huh?, wtf"
And he can ask questions. That's how it works in most places. See something you don't understand, ask about it, get an answer, and learn. But here it's all about never giving away 'trade secrets' and assuming that everyone who isn't a bitter old guru is a thieving plagiarist scumbag.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,759
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 134
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Currency Converer (Need help with this)

 
0
  #17
Jul 1st, 2009
Originally Posted by Tom Gunn View Post
If the code is already there, you might as well help make it better and hope the guy learns something instead of sitting on your **** waiting for him to fail at life.
or, conversely:

you post your code there, and wait for someone to make it better and hope some guy doesn't expect you to actually learn something, and you can just sit on your ass waiting for handouts all your life


Originally Posted by Tom Gunn View Post
And he can ask questions. That's how it works in most places. See something you don't understand, ask about it, get an answer, and learn.
O RLY?

and where are these "most places" of which you speak? The ones where people ask questions to get answers? Because it sure isn't here.

DoEds
Newbie Poster
Posts: 1



.
Last edited by jephthah; Jul 1st, 2009 at 1:20 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 4,210
Reputation: WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future WaltP has a brilliant future 
Solved Threads: 400
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Industrious Poster

Re: Currency Converer (Need help with this)

 
1
  #18
Jul 2nd, 2009
Enough, people. The facts are
1) fixed code should not have been posted by anyone other than the OP
2) fixed code should not have been improved by anyone unless the OP posted correct code in the first place.
3) People who expect the world to give them a free ride deserve the right to fail, and do not deserve the right to pass.

Now stop the flaming and get back on track.
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: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 17
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Currency Converer (Need help with this)

 
0
  #19
Jul 2nd, 2009
Woah.. Sorry guy's, didn't mean to start a war
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 49
Reputation: DoEds is an unknown quantity at this point 
Solved Threads: 0
DoEds DoEds is offline Offline
Light Poster

Re: Currency Converer (Need help with this)

 
0
  #20
Jul 2nd, 2009
Oh man i got it working thanks a lot...
Sorry for very late reply i was so busy at the moment...
I can only surf to internet during night time...

This so cool...It's my first time to join programming community (well, daniweb is the first one)...And i got lot of replies...
I'm gonna love this site...

I really had a hard time in programming 'coz our instructor was giving first an activity before he explains everything...

That's really kinda odd >,<
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1445 | Replies: 21
Thread Tools Search this Thread



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

©2003 - 2010 DaniWeb® LLC