C++ 中的错误结果

Wrong result in c++

我想编码货币转换器并且我做到了, 但是,当我尝试时,结果比我预期的要错误, 知道代码有效,但结果不正确,我尝试了所有我知道的方法 ,我是初学者,请回答我的问题,谢谢

#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;
}
#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;
}

不要使用“using namespace std;”那是不好的做法,为什么在某些情况下你要除法,这是一个简单的数学。我希望你得到你的解决方案