Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Src/Currency_Converter/CurrencyConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "CurrencyConverter.h"
#include <iostream>
#include <stdexcept>

CurrencyConverter::CurrencyConverter() {
// We will use USD as the "base currency" for all conversions.
// These rates represent how many USD 1 unit of the currency is worth.
// Example: 1 EUR = 1.08 USD
rates["USD"] = 1.00;
rates["EUR"] = 1.08;
rates["INR"] = 0.012;
rates["GBP"] = 1.25;
rates["JPY"] = 0.0067;
}

bool CurrencyConverter::isCurrencySupported(const std::string& currencyCode) {
return rates.find(currencyCode) != rates.end();
}

// Helper function to print all supported currency codes
void CurrencyConverter::printAvailableCurrencies() {
std::cout << "Supported currencies: ";
for (const auto& pair : rates) {
std::cout << pair.first << " "; // pair.first is the key (e.g., "USD")
}
std::cout << std::endl;
}

// The main conversion logic
double CurrencyConverter::convert(double amount, const std::string& fromCurrency, const std::string& toCurrency) {

// 1. Error Handling: Check if both currencies are supported
if (!isCurrencySupported(fromCurrency)) {
throw std::invalid_argument("Error: 'From' currency code not supported: " + fromCurrency);
}
if (!isCurrencySupported(toCurrency)) {
throw std::invalid_argument("Error: 'To' currency code not supported: " + toCurrency);
}

// 2. Handle simple case: No conversion needed
if (fromCurrency == toCurrency) {
return amount;
}

// 3. Conversion Logic (using USD as a base)
// First, convert the 'from' amount into our base currency (USD)
double amountInUSD = amount * rates[fromCurrency];

// Second, convert from USD into the 'to' currency
// (We divide because the rate is X_USD = 1_TARGET_CURRENCY)
// To get 1 USD = Y_TARGET_CURRENCY, we do 1 / rate.
double finalAmount = amountInUSD / rates[toCurrency];

return finalAmount;
}
26 changes: 26 additions & 0 deletions Src/Currency_Converter/CurrencyConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <map>

class CurrencyConverter {
private:
// A map to store exchange rates, with USD as the base currency.
// The key is the currency code (e.g., "EUR"), and the value is
// how many USD 1 unit of that currency is worth.
std::map<std::string, double> rates;

public:
// This is the constructor. It's called when you create a new
// CurrencyConverter object. We use it to load our predefined rates.
CurrencyConverter();

// The main conversion function.
// Throws an exception if a currency code is invalid.
double convert(double amount, const std::string& fromCurrency, const std::string& toCurrency);

// A helper function to check if we support a currency.
bool isCurrencySupported(const std::string& currencyCode);

// A helper function to show the user what currencies are available.
void printAvailableCurrencies();
};
45 changes: 45 additions & 0 deletions Src/Currency_Converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,48 @@ The program is designed using **Object-Oriented Programming (OOP)** principles,
- Clear and readable methods

---

# How to Compile and Run

## Prerequisites
You will need a C++ compiler installed on your system, such as **g++** (part of the **MinGW-w64** toolset on Windows) or **Clang**.
Make sure it is added to your system's **PATH**.

---

## Steps

1. **Open your terminal or command prompt.**

2. **Navigate to the project directory** where the `.cpp` files are located:

```bash
cd path/to/Currency_Converter
```

3. **Compile and Run**
# Compile the source code
Run the following command in your terminal:

```bash
g++ main.cpp CurrencyConverter.cpp -o currency_converter -std=c++11
```

This will compile all the necessary files and create a single executable program named
`currency_converter` (or `currency_converter.exe` on Windows).

---

# Run the Application

**On Windows (PowerShell/CMD):**
```bash
.\currency_converter.exe
```

**On macOS or Linux:**
```bash
./currency_converter
```

Follow the on-screen prompts to use the converter.
Binary file added Src/Currency_Converter/currency_converter.exe
Binary file not shown.
89 changes: 89 additions & 0 deletions Src/Currency_Converter/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <string>
#include <limits>
#include <stdexcept>

#include "CurrencyConverter.h"

double getValidAmount() {
double amount;
while (true) {
std::cout << "Enter amount to convert: ";
if (std::cin >> amount) {
if (amount > 0) {
return amount;
} else {
std::cout << "Please enter a positive amount." << std::endl;
}
} else {
// This handles non-numeric input
std::cout << "Invalid input. Please enter a number." << std::endl;
std::cin.clear(); // Clear the error flag
// Discard the invalid input from the buffer
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}

// Helper function to get a valid currency code from the user
std::string getValidCurrency(const std::string& prompt, CurrencyConverter& converter) {
std::string code;
while (true) {
std::cout << prompt;
std::cin >> code;

// Convert input to uppercase to be safe
for (auto &c : code) c = toupper(c);

if (converter.isCurrencySupported(code)) {
return code; // Valid currency
} else {
std::cout << "Invalid or unsupported currency code. Please try again." << std::endl;
converter.printAvailableCurrencies();
}
}
}


int main() {
// 1. Create an instance of our converter.
// This calls the constructor and loads the rates.
CurrencyConverter converter;

std::cout << "Welcome to the C++ Currency Converter!" << std::endl;

// 2. Main application loop
while (true) {
std::cout << "\n------------------------------------" << std::endl;
converter.printAvailableCurrencies();

// 3. Get all user inputs
std::string from = getValidCurrency("Convert FROM (e.g., USD): ", converter);
std::string to = getValidCurrency("Convert TO (e.g., EUR): ", converter);
double amount = getValidAmount();

// 4. Perform conversion and handle errors
try {
double result = converter.convert(amount, from, to);

// Print the result
std::cout << "\n--- Result ---" << std::endl;
std::cout << amount << " " << from << " = " << result << " " << to << std::endl;

} catch (const std::exception& e) {
// This will catch errors we threw in our .convert() function
std::cout << e.what() << std::endl;
}

// 5. Ask to go again
std::cout << "\nDo you want to perform another conversion? (y/n): ";
char choice;
std::cin >> choice;
if (choice != 'y' && choice != 'Y') {
break; // Exit the while loop
}
}

std::cout << "Thank you for using the converter. Goodbye!" << std::endl;
return 0;
}
Loading