fork download
  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4.  
  5. static const double USD_TO_MXN = 12.99;
  6. static const double GBP_TO_MXN = 22.03;
  7. static const char DATA_DATE[] = "6/12/2014";
  8.  
  9. void convert(const char* from, const char* to, double exchange)
  10. {
  11. cout << "Enter the number of " << from << " to convert to " << to << ".\n"
  12. "Amount: ";
  13. int original;
  14. cin >> original;
  15. cout << '\n' << original << ' ' << from << " gives " << int(original * exchange) << ' ' << to << ".\n";
  16. }
  17.  
  18. int main() // this is valid since C++2003
  19. {
  20. cout << "Foreign Currency Converter\n"
  21. "1 USD = " << USD_TO_MXN << " MXN (" << DATA_DATE << ")\n"
  22. "1 GBP = " << GBP_TO_MXN << " MXN (" << DATA_DATE << ")\n"
  23. "\n";
  24.  
  25. for ( ; ; ) { // continuous loop
  26. cout << "Which conversion do you want to perform?\n"
  27. "[1] USD to MXN\n"
  28. "[2] MXN to USD\n"
  29. "[3] GBP to MXN\n"
  30. "[4] MXN to GBP\n"
  31. "[0] Quit\n"
  32. "Selection: ";
  33. int choice = -1;
  34. cin >> choice;
  35. cout << '\n';
  36.  
  37. switch (choice) {
  38. case 0:
  39. return 0; // return from main
  40.  
  41. case 1:
  42. convert("US Dollars", "Pesos", USD_TO_MXN);
  43. break;
  44.  
  45. case 2:
  46. convert("Pesos", "US Dollars", 1 / USD_TO_MXN);
  47. break;
  48.  
  49. case 3:
  50. convert("British Pounds", "Pesos", GBP_TO_MXN);
  51. break;
  52.  
  53. case 4:
  54. convert("Pesos", "British Pounds", 1 / GBP_TO_MXN);
  55. break;
  56.  
  57. default:
  58. cout << "Invalid selection. Try again.\n";
  59. }
  60. }
  61. }
  62.  
Success #stdin #stdout 0s 3304KB
stdin
1
5
2
130
3
9
4
221
0
stdout
Foreign Currency Converter
1 USD = 12.99 MXN (6/12/2014)
1 GBP = 22.03 MXN (6/12/2014)

Which conversion do you want to perform?
[1] USD to MXN
[2] MXN to USD
[3] GBP to MXN
[4] MXN to GBP
[0] Quit
Selection: 
Enter the number of US Dollars to convert to Pesos.
Amount: 
5 US Dollars gives 64 Pesos.
Which conversion do you want to perform?
[1] USD to MXN
[2] MXN to USD
[3] GBP to MXN
[4] MXN to GBP
[0] Quit
Selection: 
Enter the number of Pesos to convert to US Dollars.
Amount: 
130 Pesos gives 10 US Dollars.
Which conversion do you want to perform?
[1] USD to MXN
[2] MXN to USD
[3] GBP to MXN
[4] MXN to GBP
[0] Quit
Selection: 
Enter the number of British Pounds to convert to Pesos.
Amount: 
9 British Pounds gives 198 Pesos.
Which conversion do you want to perform?
[1] USD to MXN
[2] MXN to USD
[3] GBP to MXN
[4] MXN to GBP
[0] Quit
Selection: 
Enter the number of Pesos to convert to British Pounds.
Amount: 
221 Pesos gives 10 British Pounds.
Which conversion do you want to perform?
[1] USD to MXN
[2] MXN to USD
[3] GBP to MXN
[4] MXN to GBP
[0] Quit
Selection: