C ++货币转换代码

时间:2014-09-13 15:07:36

标签: c++

您好我是c ​​+的新手,并且很难创建转换货币的代码。你能看一下我目前的代码并提出任何建议。第一个目标是首先确定货币的类型。然后金额。最后是转换。

#include <iostream>
#include <string> 
using namespace std;
int main()

{

    //declaring constant conversion values of currency per dollar
    const float ColombianPeso = 2000;
    const float MexicanPeso = 13.25;
    const float ArgentinePeso = 8.4;
    const float VenesuelanBolivar = 6.28;
    const float ChileanPeso = 593.719;

    //designing statement to allow user to input curency type

    char currency[] = "show me the money (USDollar, MexicanPeso, ArgentinePeso, ColombianPeso, VenesuelanBolivar, or ChileanPeso):\n";
    char answer1[17];
    cout << currency;
    cin >> answer1;

    //designing statement to imput amount
    float amount = 0;   
    cout << "enter amount:\n";
    cin >> amount;

    //creating if/else statement to convert for diffent money values

    if (answer1 == USDollar)
    cout << "number of Colombian Pesos:\n" << amount * ColombianPeso;
    cout << "number of Venesuelan Bolivars:\n" << amount * VenesuelanBolivar;
    cout << "number of Mexican Pesos:\n" << amount * MexicanPeso;
    cout << "number of Argentine Pesos:\n" << amount * ArgentinePeso;
    cout << "number of Chilean Pesos:\n" << amount * ChileanPeso;
    else if (answer1 == MexicanPeso)
    cout << "number of US Dollars:\n" << amount / MexicanPeso;
    else if (answer1 == ColombianPeso)
    cout << "number of US Dollars:\n" << amount / ColombianPeso;
    else if (answer1 == ArgentinePeso)
    cout << "number of US Dollars:\n" << amount / ArgentinePeso;
    else if (answer1 == ChileanPeso)
    cout << "number of US Dollars:\n" << amount / ChileanPeso;  
    else if (answer1 == VenesuelanBolivar)
    cout << "number of US Dollars:\n" << amount / VenesuelanBolivar;
    else
    cout << "try again with VenesuelanBolivar, USDollar, ChileanPeso, ArgentinePeso, ColombianPeso, or MexicanPeso:\n";
    return 0;

}

2 个答案:

答案 0 :(得分:2)

使用std::string代替char[]。你还需要比较字符串文字,否则它会认为这些是变量。

std::string answer1;
cin >> answer1;

if (answer1 == "USDollar")
{
    // do stuff
}

答案 1 :(得分:0)

  1. 您正在比较char[]float,我认为您并不想要它。

    const float ColombianPeso = 2000;
    //...
    char answer1[17];
    //...
    cin >> answer1;
    //...
    else if (answer1 == ColombianPeso)
    
  2. 我建议您使用std::unordered_map;它允许您在给出字符串时获得float用于转换的示例。此外,它允许在运行时修改货币及其比率。

    #include <string>
    #include <unordered_map>
    #include <iostream>
    #include <stdexcept>
    using std::cout;
    using std::cin;
    using std::clog;
    int main(){
        std::unordered_map<std::string, float> ratio{
            {"ColombianPeso",   2000},
            {"MexicanPeso",       13.45},
            {"ArgentinePeso",      8.4},
            {"VenesueleanBolivar", 6.28},
            {"ChileanPeso",      593.719}
        };
        clog << "Available currency:\n";
        for(auto it=ratio.cbegin(); it!=ratio.cend(); ++i)
            clog<< '\t' << it->first << ' ' << it->second << '\n';
        clog << "Pick one of currency above: ";
        std::string choice;
        cin  >> choice;
        try{
            auto value=ratio.at(choice); /*if element doesn't exist,
                                          program jumps to the catch*/
            float amount;
            clog >> "Enter amount: ";
            cin  >> amount;
            cout << value*amount << '\n';
        }catch(std::out_of_range&){
            clog << "Given currency is not available.\n";
        }
    }