943,910 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1784
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 1st, 2009
-1

Re: Currency Converer (Need help with this)

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. }
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 1st, 2009
1

Re: Currency Converer (Need help with this)

I see the OP got their wish by successive approximation without having to put in any more effort.

Congrats to all
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 1st, 2009
0

Re: Currency Converer (Need help with this)

Click to Expand / Collapse  Quote originally posted by Salem ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 1st, 2009
0

Re: Currency Converer (Need help with this)

>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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jul 1st, 2009
0

Re: Currency Converer (Need help with this)

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 1st, 2009
-1

Re: Currency Converer (Need help with this)

Quote ...
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.

Quote ...
Why not nicely pack in a parcel with automatic installers and send him on his postal address with a Project website built?
Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 1st, 2009
0

Re: Currency Converer (Need help with this)

Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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


Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
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 2:20 pm.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 2nd, 2009
1

Re: Currency Converer (Need help with this)

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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is online now Online
7,733 posts
since May 2006
Jul 2nd, 2009
0

Re: Currency Converer (Need help with this)

Woah.. Sorry guy's, didn't mean to start a war
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Jul 2nd, 2009
0

Re: Currency Converer (Need help with this)

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 >,<
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Cant run the code using gcc
Next Thread in C Forum Timeline: Reading A File





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


Follow us on Twitter


© 2011 DaniWeb® LLC