Search code examples
c++currencyconverters

Wrong result in c++


I want to code currency converter and I do it, but, When I tried it, result was wrong than I expected, Knowing that the code works, but the result is not correct and I tried all the methods I know , I am a beginner, please a answer my question, and Thanks

#include <iostream>
using namespace std;

int main()
{
    cout << "1- EGP TO Usd" << endl;
    cout << "2- USD TO EGP" << endl;
    cout << "3- EGP TO SAR" << endl;
    cout << "4- USD TO SAR" << endl;
    cout << "5- SAR TO EGP" << endl;
    cout << "6- SAR TO USD" << endl;

    int num;
    double balance;

    double egpToUsd = .064;
    double usdToEgp = 15.70;
    double egpToSar = .24;
    double usdToSar = 3.75;
    double sarToEgp = 4.19;
    double sarToUsd = .27;

    cout << "Enter a Num: ";
    cin >> num;
    cout << "Enter your balance: ";
    cin >> balance;

    if (num >= 1 && num <= 6)
    {
        switch (num)
        {
        case 1:
            cout << (balance / egpToUsd);
            break;
        case 2:
            cout << (balance * usdToEgp);
            break;
        case 3:
            cout << (balance / egpToSar);
            break;
        case 4:
            cout << (balance * usdToSar);
            break;
        case 5:
            cout << (balance * sarToEgp);
            break;
        case 6:
            cout << (balance / sarToUsd);
            break;
        }
    }
    else {
        cout << "Error please select from list" << endl;
    }

    return 0;
}

Solution

  • #include <iostream>
    
    int main()
    {
        std::cout << "1- EGP TO Usd\n";
        std::cout << "2- USD TO EGP\n";
        std::cout << "3- EGP TO SAR\n";
        std::cout << "4- USD TO SAR\n";
        std::cout << "5- SAR TO EGP\n";
        std::cout << "6- SAR TO USD\n";
    
        int num;
        double balance;
    
        double egpToUsd = .064;
        double usdToEgp = 15.70;
        double egpToSar = .24;
        double usdToSar = 3.75;
        double sarToEgp = 4.19;
        double sarToUsd = .27;
    
        std::cout << "Enter a Num: ";
        std::cin >> num;
        std::cout << "Enter your balance: ";
        std::cin >> balance;
    
            switch (num)
            {
            case 1:
                std::cout << (balance * egpToUsd);
                break;
            case 2:
                std::cout << (balance * usdToEgp);
                break;
            case 3:
                std::cout << (balance * egpToSar);
                break;
            case 4:
                std::cout << (balance * usdToSar);
                break;
            case 5:
                std::cout << (balance * sarToEgp);
                break;
            case 6:
                std::cout << (balance * sarToUsd);
                break;
            default:
                std::cout << "Error please select from list\n";
                break;
            }    
        return 0;
    }
    

    Dont Use "using namespace std;" thats bad practice and why were you dividing in some cases, its a simple math. I hope you got your solution