fork download
  1. #include <iostream>
  2.  
  3. static const double US_TO_MXN = 12.99;
  4. static const char DATA_DATE[] = "6/12/2014";
  5.  
  6. void convert(const char* from, const char* to, double exchange)
  7. {
  8. std::cout << "Enter the number of " << from << " to convert to " << to << ".\n"
  9. "Amount: ";
  10. int original;
  11. std::cin >> original;
  12. std::cout << to << ": " << (original * exchange) << '\n';
  13. }
  14.  
  15. int main() // this is valid since C++2003
  16. {
  17. std::cout << "US/MXN Converter\n"
  18. "1 US = " << US_TO_MXN << " MXN (" << DATA_DATE << ")\n"
  19. "\n";
  20.  
  21. int choice = 0;
  22. // Here's a better demonstration of goto
  23. GET_CHOICE:
  24. std::cout << "Which conversion do you want to perform?\n"
  25. "[1] US to MXN\n"
  26. "[2] MXN to US\n"
  27. "Selection: ";
  28. std::cin >> choice;
  29.  
  30. if (choice == 1)
  31. convert("US Dollars", "Pesos", US_TO_MXN);
  32. else if (choice == 2)
  33. convert("Pesos", "US Dollars", 1 / US_TO_MXN);
  34. else {
  35. std::cerr << "Invalid choice. Please try again.\n";
  36. goto GET_CHOICE;
  37. }
  38.  
  39. // this also serves to demonstrate that goto is bad because
  40. // it's not obvious from the above that you have a loop.
  41. }
  42.  
Success #stdin #stdout 0s 3344KB
stdin
1
20
stdout
US/MXN Converter
1 US = 12.99 MXN (6/12/2014)

Which conversion do you want to perform?
[1] US to MXN
[2] MXN to US
Selection: Enter the number of US Dollars to convert to Pesos.
Amount: Pesos: 259.8