Currency Converter

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Dave Sinkula Dave Sinkula is offline Offline May 16th, 2005, 2:57 pm |
0
This problem is a typical homework question. The code presented here may contain some elements that may be a little "ahead of the game" in coursework, but it presents some of the basic elements that might be used in such an exercise.
Quick reply to this message  
C Syntax
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const char Description[] =
  5. "Currency Conversion As of June 11, 2003 2:35am GMT";
  6.  
  7. void convert(double amount, const char *from, const char *to)
  8. {
  9. static const struct
  10. {
  11. const char *name;
  12. double rate;
  13. } Xchg[] = /* US Dollar conversion factor */
  14. {
  15. { "USD", 1.0 },
  16. { "Lire", 0.000603049 },
  17. { "Yen", 0.008464602 },
  18. { "Pound", 1.65123 },
  19. { "Peso", 0.0940910 },
  20. { "Canadian", 0.734061 },
  21. };
  22. size_t i,j;
  23. /*
  24.   * Search for the name of the currency you want to convert from.
  25.   */
  26. for ( i = 0; i < sizeof Xchg / sizeof *Xchg; ++i )
  27. {
  28. if ( strcmp(Xchg[i].name, from) == 0 )
  29. {
  30. /*
  31.   * Found the 'from' currency name; 'i' is the index of it.
  32.   * Search for the name of the currency you want to convert to.
  33.   */
  34. for ( j = 0; j < sizeof Xchg / sizeof *Xchg; ++j )
  35. {
  36. if ( strcmp(Xchg[j].name, to) == 0 )
  37. {
  38. /*
  39.   * Found the 'to' currency name; 'j' is the index of it.
  40.   * Calculate the new value: multiply by the 'from' rate and
  41.   * divide by the 'to' rate.
  42.   */
  43. double value = amount * Xchg[i].rate / Xchg[j].rate;
  44. printf("%g %s = %g %s\n", amount, from, value, to);
  45. return;
  46. }
  47. }
  48. printf("Cannot convert to \"%s\"\a\n", to); /* 'to' name not found */
  49. return;
  50. }
  51. }
  52. printf("Cannot convert from \"%s\"\a\n", from); /* 'from' name not found */
  53. }
  54.  
  55. int main(void)
  56. {
  57. puts(Description);
  58. convert( 20.0, "USD", "Pound");
  59. convert(100.0, "Yen", "Lire" );
  60. convert( 20.0, "Euro", "Krone");
  61. convert( 20.0, "USD", "Krone");
  62. return 0;
  63. }
  64.  
  65. /* my output
  66. Currency Conversion As of June 11, 2003 2:35am GMT
  67. 20 USD = 12.1122 Pound
  68. 100 Yen = 1403.63 Lire
  69. Cannot convert from "Euro"
  70. Cannot convert to "Krone"
  71. */
0
bumsfeld bumsfeld is offline Offline | Aug 1st, 2005
David, you know how to explane things good!
 
0
Sealteam56 Sealteam56 is offline Offline | Jul 6th, 2006
Hello,

I am new to this forum but noticed you all seem very helpful. I am learning C-programming and been given a task to complete a currency conversion program. I have about exhasuted all my level of knowledge and have used up the books I have as well.

If someone here could please review this program and perhaps let me know what I have done wrong here that would be great.

The issue is that the program is to allow for a user to select a currency value from the displayed list, which as far as I can tell it will.

Than the user is to input a value to be converted, it is here that I have missed something as everytime I now enter a value I get back my error message which was coded, at least was my plan to check for only numeric values.

Again any help would be great!!!

  1.  
  2. /*The conversion rates of five currencies equivalent to one US dollar*/
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(void)
  7. {
  8. int choice;
  9. int currency;
  10. float USD, UK, HK, EUR, NZ, AUS;
  11. char buffer[30];
  12.  
  13. /********************************************
  14.   ******Current Currency Exchange Rates*******
  15.   *
  16.   * British Pound(UK) 0.5408 6/15 8:50pm
  17.   * Hong Kong Dollar (HK) 7.7617 6/15 8:50pm
  18.   * Euro (EUR) 0.7914 6/15 8:50pm
  19.   * New Zealand (NZ) Dollar 1.6079 6/15 8:50pm
  20.   * Australian Dollar (AUS) 1.3556 6/15 8:50pm
  21.   * United States Dollar (US)
  22.   *
  23.   **********************************************/
  24.  
  25.  
  26. USD = 1.00;
  27. UK = 0.5408;
  28. HK = 7.7617;
  29. EUR = 0.7914;
  30. NZ = 1.6079;
  31. AUS = 1.3556;
  32.  
  33. do // main loop runs until choice 0 is entered
  34. {
  35. /*Version and title of program and menu selection*/
  36.  
  37. printf("\n\n Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
  38.  
  39. printf("Please select the currency type you would like to convert into US dollars.\n\n");
  40.  
  41. printf("1. for British Pounds \n");
  42. printf("2. for Hong Kong Dollars \n");
  43. printf("3. for Euros \n");
  44. printf("4. for New Zealand Dollars \n");
  45. printf("5. for Australian Dollars \n\n");
  46.  
  47.  
  48.  
  49. printf("Press 0 and then return to exit the Currency Converter \n");
  50.  
  51. choice = 6; /* must be initialized to an invalid value*/
  52. /* it's not initialized the first time, it might be in the range of 1..5*/
  53. do /* 1st input loop for the choice*/
  54. {
  55. printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
  56. /*reads the characters from the file associated with fp into the string pointed to.*/
  57. fgets(buffer, sizeof(buffer), stdin);
  58. /* Error Checking for Numeric Values only*/
  59. if ( ( sscanf(buffer, "%u", &choice) == 0) || choice > 5 || choice < 0 )
  60.  
  61. {
  62. printf("\n\n\a Error please select agian \n");
  63. }
  64. }
  65. while(choice < 0 || choice > 5);
  66.  
  67. if ( choice > 0 )
  68. { /* no need to ask for the amount if exit was selected*/
  69.  
  70. currency = 100000; // see above
  71.  
  72. do /* 2nd input loop for the amount*/
  73. {
  74.  
  75. printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
  76.  
  77. fgets(buffer, sizeof(buffer), stdin);
  78.  
  79. /* Error Checking for Numeric Values only*/
  80. if ( ( sscanf(buffer, "%d", &currency) == 0) || currency > 99999 || currency < 1 )
  81. {
  82. printf("\n\n\a This is an Errneous amount based on your input\n");
  83. printf(" please enter a numeric value");
  84. }
  85. }
  86. while(currency > 99999 || currency < 1); /* changed the condition to int values*/
  87. }
  88.  
  89. switch(choice)
  90. {
  91.  
  92. case 1:
  93. printf("\n\n You will have %.2f US dollars \n\n", USD/UK*currency);
  94. break;
  95. case 2:
  96. printf("\n\n You will have %.2f US dollars \n\n", USD/HK*currency);
  97. break;
  98. case 3:
  99. printf("\n\n You will have %.2f US dollars\n\n", USD/EUR*currency);
  100. break;
  101. case 4:
  102. printf("\n\n You will have %.2f US dollars \n\n", USD/NZ*currency);
  103. break;
  104. case 5:
  105. printf("\n\n You will have %.2f US dollars \n\n", USD/AUS*currency);
  106. break;
  107. case 0:
  108. break;
  109. return 0;
  110. }
  111.  
  112. }
  113.  
  114. while(choice!=0 && currency != 0); /* saves the second switch.*/
  115. /* the switch would not work if I had changed currency to float*/
  116. puts("bye.");
  117. puts("press enter.");
  118. getchar();
  119.  
  120. return 0;
  121. }


I think what is possibly the error is that the value inputted is not viewed by the code as a valid input no matter what is entered.

Allow me to thank you now for your help.

Randy
:rolleyes:
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC