Coorect the Following C++ code: #include #include #include #include #include using namespace std; // Function to print available currencies for exchange void printCurrencies() {     cout << "Available currencies for exchange: " << endl;     cout << "SAR --> Saudi Arabia Riyal" << endl;     cout << "KWD --> Kuwaiti Dinar" << endl;     cout << "QAR --> Qatar Riyal" << endl;     cout << "AED --> United Arab Emirates Dirham" << endl;     cout << "BHD --> Bahraini Dinar" << endl;     cout << "OMR --> Omani Rial" << endl; } // Function to convert currency double convertCurrency(string fromCurrency, string toCurrency, double amount) {     ifstream exchangeRateFile("ExchangeRate.txt"); // Open file for reading     string line;          double desiredRate=0;          while (getline(exchangeRateFile, line)) { // Read file line by line         int i=0;         string from, rate, to;          while (line[i] != ' ') {             i++;         }         i++;         while (line[i] != ' ') {             from += line[i];             i++;         }         i++;         while (line[i] != ' ') {             rate += line[i];             i++;         }         i++;         while (i < line.length()) {             to += line[i];             i++;         }         if(from==fromCurrency && to.substr(0,to.length()-1)==toCurrency){             desiredRate=stod(rate);             break;         }     }     exchangeRateFile.close(); // Close file     return amount * desiredRate; // Return converted amount } string toupper(string currency){     string s="";     for (char c : currency) {         s += toupper(c);     }     return s; } int main() {     printCurrencies(); // Print available currencies     string fromCurrency, toCurrency;     double amount;     cout << "Enter the currency you want to convert from: ";     cin >>fromCurrency;     fromCurrency = toupper(fromCurrency);     while(fromCurrency != "SAR" && fromCurrency != "KWD" && fromCurrency != "QAR" && fromCurrency != "AED" && fromCurrency != "BHD" && fromCurrency != "OMR"){         cout << "Invalid input, Please Enter again: ";               cin >>fromCurrency;         fromCurrency = toupper(fromCurrency);     }     cout<< "Enter the currency you want to convert to: ";         cin >>toCurrency;     toCurrency = toupper(toCurrency);     while(toCurrency != "SAR" && toCurrency != "KWD" && toCurrency != "QAR" && toCurrency != "AED" && toCurrency != "BHD" && toCurrency != "OMR"){         cout<< "Invalid input, Please Enter again: ";         cin >>toCurrency;         toCurrency = toupper(toCurrency);     }     cout<<"Enter the amount you want to convert: ";      cin >> amount;     cout << amount << " " << fromCurrency << " = " << convertCurrency(fromCurrency, toCurrency, amount) << " " << toCurrency << endl;     return 0; }       to get the below result: Sample run1: The currencies we have exchange for are: SAR--> Saudi Arabia Riyal KWD --> Kuwaiti Dinar QAR --> Qatar Riyal AED--> United Arab Emirates Dirham BHD --> Bahraini Dinar OMR --> Omani Rial What is the currency you have? SaR What is the currency you want to exchange it for? aed How much money you want to exchange? 707 707 SAR is 692.86 AED    Sample run2: The currencies we have exchange for are: SAR --> Saudi Arabia Riyal KWD --> Kuwaiti Dinar QAR --> Qatar Riyal AED --> United Arab Emirates Dirham BHD --> Bahraini Dinar OMR--> Omani Rial What is the currency you have? Kuwaiti Incorrect abbreviation please provide it again Sad Incorrect abbreviation please provide it again SAR What is the currency you want to exchange it for? BHR Incorrect abbreviation please provide it again BHD How much money you want to exchange? 50 50 SAR is 5 BHD

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.1: Function And Parameter Declarations
Problem 11E
icon
Related questions
Question

Coorect the Following C++ code:

#include <bits/stdc++.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

using namespace std;

// Function to print available currencies for exchange
void printCurrencies() {
    cout << "Available currencies for exchange: " << endl;
    cout << "SAR --> Saudi Arabia Riyal" << endl;
    cout << "KWD --> Kuwaiti Dinar" << endl;
    cout << "QAR --> Qatar Riyal" << endl;
    cout << "AED --> United Arab Emirates Dirham" << endl;
    cout << "BHD --> Bahraini Dinar" << endl;
    cout << "OMR --> Omani Rial" << endl;
}

// Function to convert currency
double convertCurrency(string fromCurrency, string toCurrency, double amount) {
    ifstream exchangeRateFile("ExchangeRate.txt"); // Open file for reading
    string line;
    
    double desiredRate=0;
    
    while (getline(exchangeRateFile, line)) { // Read file line by line
        int i=0;
        string from, rate, to;
         while (line[i] != ' ') {
            i++;
        }
        i++;
        while (line[i] != ' ') {
            from += line[i];
            i++;
        }
        i++;
        while (line[i] != ' ') {
            rate += line[i];
            i++;
        }
        i++;
        while (i < line.length()) {
            to += line[i];
            i++;
        }
        if(from==fromCurrency && to.substr(0,to.length()-1)==toCurrency){
            desiredRate=stod(rate);
            break;
        }
    }
    exchangeRateFile.close(); // Close file
    return amount * desiredRate; // Return converted amount
}

string toupper(string currency){
    string s="";
    for (char c : currency) {
        s += toupper(c);
    }
    return s;
}

int main() {
    printCurrencies(); // Print available currencies

    string fromCurrency, toCurrency;
    double amount;

    cout << "Enter the currency you want to convert from: ";
    cin >>fromCurrency;
    fromCurrency = toupper(fromCurrency);
    while(fromCurrency != "SAR" && fromCurrency != "KWD" && fromCurrency != "QAR" && fromCurrency != "AED" && fromCurrency != "BHD" && fromCurrency != "OMR"){
        cout << "Invalid input, Please Enter again: ";      
        cin >>fromCurrency;
        fromCurrency = toupper(fromCurrency);
    }

    cout<< "Enter the currency you want to convert to: ";    
    cin >>toCurrency;
    toCurrency = toupper(toCurrency);
    while(toCurrency != "SAR" && toCurrency != "KWD" && toCurrency != "QAR" && toCurrency != "AED" && toCurrency != "BHD" && toCurrency != "OMR"){
        cout<< "Invalid input, Please Enter again: ";
        cin >>toCurrency;
        toCurrency = toupper(toCurrency);
    }

    cout<<"Enter the amount you want to convert: "; 
    cin >> amount;

    cout << amount << " " << fromCurrency << " = " << convertCurrency(fromCurrency, toCurrency, amount) << " " << toCurrency << endl;

    return 0;
}

 

 

 

to get the below result:

Sample run1:

The currencies we have exchange for are:

SAR--> Saudi Arabia Riyal

KWD --> Kuwaiti Dinar

QAR --> Qatar Riyal

AED--> United Arab Emirates Dirham

BHD --> Bahraini Dinar

OMR --> Omani Rial

What is the currency you have? SaR

What is the currency you want to exchange it for?

aed

How much money you want to exchange?

707

707 SAR is 692.86 AED

 

 Sample run2:
The currencies we have exchange for are:
SAR --> Saudi Arabia Riyal
KWD --> Kuwaiti Dinar
QAR --> Qatar Riyal
AED --> United Arab Emirates Dirham
BHD --> Bahraini Dinar
OMR--> Omani Rial
What is the currency you have? Kuwaiti
Incorrect abbreviation please provide it again
Sad
Incorrect abbreviation please provide it again
SAR
What is the currency you want to exchange it for?
BHR
Incorrect abbreviation please provide it again
BHD
How much money you want to exchange?
50
50 SAR is 5 BHD

Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr