Skip to content

Commit 1525da1

Browse files
feat: Implement C++ Currency Converter
Added a C++ Currency Converter feature with OOP-based design, supporting multi-currency conversion, user-friendly console interface, and error handling. Closes #4
1 parent fcd5a2a commit 1525da1

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "CurrencyConverter.h"
2+
#include <iostream>
3+
#include <stdexcept>
4+
5+
CurrencyConverter::CurrencyConverter() {
6+
// We will use USD as the "base currency" for all conversions.
7+
// These rates represent how many USD 1 unit of the currency is worth.
8+
// Example: 1 EUR = 1.08 USD
9+
rates["USD"] = 1.00;
10+
rates["EUR"] = 1.08;
11+
rates["INR"] = 0.012;
12+
rates["GBP"] = 1.25;
13+
rates["JPY"] = 0.0067;
14+
}
15+
16+
bool CurrencyConverter::isCurrencySupported(const std::string& currencyCode) {
17+
return rates.find(currencyCode) != rates.end();
18+
}
19+
20+
// Helper function to print all supported currency codes
21+
void CurrencyConverter::printAvailableCurrencies() {
22+
std::cout << "Supported currencies: ";
23+
for (const auto& pair : rates) {
24+
std::cout << pair.first << " "; // pair.first is the key (e.g., "USD")
25+
}
26+
std::cout << std::endl;
27+
}
28+
29+
// The main conversion logic
30+
double CurrencyConverter::convert(double amount, const std::string& fromCurrency, const std::string& toCurrency) {
31+
32+
// 1. Error Handling: Check if both currencies are supported
33+
if (!isCurrencySupported(fromCurrency)) {
34+
throw std::invalid_argument("Error: 'From' currency code not supported: " + fromCurrency);
35+
}
36+
if (!isCurrencySupported(toCurrency)) {
37+
throw std::invalid_argument("Error: 'To' currency code not supported: " + toCurrency);
38+
}
39+
40+
// 2. Handle simple case: No conversion needed
41+
if (fromCurrency == toCurrency) {
42+
return amount;
43+
}
44+
45+
// 3. Conversion Logic (using USD as a base)
46+
// First, convert the 'from' amount into our base currency (USD)
47+
double amountInUSD = amount * rates[fromCurrency];
48+
49+
// Second, convert from USD into the 'to' currency
50+
// (We divide because the rate is X_USD = 1_TARGET_CURRENCY)
51+
// To get 1 USD = Y_TARGET_CURRENCY, we do 1 / rate.
52+
double finalAmount = amountInUSD / rates[toCurrency];
53+
54+
return finalAmount;
55+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <string>
3+
#include <map>
4+
5+
class CurrencyConverter {
6+
private:
7+
// A map to store exchange rates, with USD as the base currency.
8+
// The key is the currency code (e.g., "EUR"), and the value is
9+
// how many USD 1 unit of that currency is worth.
10+
std::map<std::string, double> rates;
11+
12+
public:
13+
// This is the constructor. It's called when you create a new
14+
// CurrencyConverter object. We use it to load our predefined rates.
15+
CurrencyConverter();
16+
17+
// The main conversion function.
18+
// Throws an exception if a currency code is invalid.
19+
double convert(double amount, const std::string& fromCurrency, const std::string& toCurrency);
20+
21+
// A helper function to check if we support a currency.
22+
bool isCurrencySupported(const std::string& currencyCode);
23+
24+
// A helper function to show the user what currencies are available.
25+
void printAvailableCurrencies();
26+
};

Src/Currency_Converter/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,48 @@ The program is designed using **Object-Oriented Programming (OOP)** principles,
2525
- Clear and readable methods
2626

2727
---
28+
29+
# How to Compile and Run
30+
31+
## Prerequisites
32+
You will need a C++ compiler installed on your system, such as **g++** (part of the **MinGW-w64** toolset on Windows) or **Clang**.
33+
Make sure it is added to your system's **PATH**.
34+
35+
---
36+
37+
## Steps
38+
39+
1. **Open your terminal or command prompt.**
40+
41+
2. **Navigate to the project directory** where the `.cpp` files are located:
42+
43+
```bash
44+
cd path/to/Currency_Converter
45+
```
46+
47+
3. **Compile and Run**
48+
# Compile the source code
49+
Run the following command in your terminal:
50+
51+
```bash
52+
g++ main.cpp CurrencyConverter.cpp -o currency_converter -std=c++11
53+
```
54+
55+
This will compile all the necessary files and create a single executable program named
56+
`currency_converter` (or `currency_converter.exe` on Windows).
57+
58+
---
59+
60+
# Run the Application
61+
62+
**On Windows (PowerShell/CMD):**
63+
```bash
64+
.\currency_converter.exe
65+
```
66+
67+
**On macOS or Linux:**
68+
```bash
69+
./currency_converter
70+
```
71+
72+
Follow the on-screen prompts to use the converter.
189 KB
Binary file not shown.

Src/Currency_Converter/main.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <limits>
4+
#include <stdexcept>
5+
6+
#include "CurrencyConverter.h"
7+
8+
double getValidAmount() {
9+
double amount;
10+
while (true) {
11+
std::cout << "Enter amount to convert: ";
12+
if (std::cin >> amount) {
13+
if (amount > 0) {
14+
return amount;
15+
} else {
16+
std::cout << "Please enter a positive amount." << std::endl;
17+
}
18+
} else {
19+
// This handles non-numeric input
20+
std::cout << "Invalid input. Please enter a number." << std::endl;
21+
std::cin.clear(); // Clear the error flag
22+
// Discard the invalid input from the buffer
23+
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
24+
}
25+
}
26+
}
27+
28+
// Helper function to get a valid currency code from the user
29+
std::string getValidCurrency(const std::string& prompt, CurrencyConverter& converter) {
30+
std::string code;
31+
while (true) {
32+
std::cout << prompt;
33+
std::cin >> code;
34+
35+
// Convert input to uppercase to be safe
36+
for (auto &c : code) c = toupper(c);
37+
38+
if (converter.isCurrencySupported(code)) {
39+
return code; // Valid currency
40+
} else {
41+
std::cout << "Invalid or unsupported currency code. Please try again." << std::endl;
42+
converter.printAvailableCurrencies();
43+
}
44+
}
45+
}
46+
47+
48+
int main() {
49+
// 1. Create an instance of our converter.
50+
// This calls the constructor and loads the rates.
51+
CurrencyConverter converter;
52+
53+
std::cout << "Welcome to the C++ Currency Converter!" << std::endl;
54+
55+
// 2. Main application loop
56+
while (true) {
57+
std::cout << "\n------------------------------------" << std::endl;
58+
converter.printAvailableCurrencies();
59+
60+
// 3. Get all user inputs
61+
std::string from = getValidCurrency("Convert FROM (e.g., USD): ", converter);
62+
std::string to = getValidCurrency("Convert TO (e.g., EUR): ", converter);
63+
double amount = getValidAmount();
64+
65+
// 4. Perform conversion and handle errors
66+
try {
67+
double result = converter.convert(amount, from, to);
68+
69+
// Print the result
70+
std::cout << "\n--- Result ---" << std::endl;
71+
std::cout << amount << " " << from << " = " << result << " " << to << std::endl;
72+
73+
} catch (const std::exception& e) {
74+
// This will catch errors we threw in our .convert() function
75+
std::cout << e.what() << std::endl;
76+
}
77+
78+
// 5. Ask to go again
79+
std::cout << "\nDo you want to perform another conversion? (y/n): ";
80+
char choice;
81+
std::cin >> choice;
82+
if (choice != 'y' && choice != 'Y') {
83+
break; // Exit the while loop
84+
}
85+
}
86+
87+
std::cout << "Thank you for using the converter. Goodbye!" << std::endl;
88+
return 0;
89+
}

0 commit comments

Comments
 (0)