C++ 货币兑换计划

C++ 货币兑换计划,c++,C++,我正在做一个货币转换程序,将旧的英镑、先令和便士转换成他们的新系统,这是一种十进制的英镑。100便士等于1英镑。这是程序的代码 #include <iostream> #include <iomanip> #include <conio.h> using namespace std; int calcNum(int pound, int shilling, int pence) { pence = pound*240 + shilling*12 +

我正在做一个货币转换程序,将旧的英镑、先令和便士转换成他们的新系统,这是一种十进制的英镑。100便士等于1英镑。这是程序的代码

#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

int calcNum(int pound, int shilling, int pence)
{
    pence = pound*240 + shilling*12 + pence;
    return pence;
}

int calcNew(int total_pence, double dec_pound)
{
    dec_pound = total_pence / 240;
    return dec_pound;
}

int main()
{

    int pence;
    int shilling;
    int pound;
    const int OLD_POUND = 240;
    const int OLD_SHILLING = 12;
    double total_pence;
    double dec_pound = 0;
    double deci_pound;

    cout << "Please Enter the Amount of old pounds: ";
    cin >> pound;
    cout << endl;
    if(cin.fail())
    {

        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    cout << "Please Enter the Amount of old shillings: ";
    cin >> shilling;
    cout << endl;
    if(cin.fail())
    {
        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    cout << "Please Enter the Amount of old pence: ";
    cin >> pence;
    cout << endl;
    if(cin.fail())
    {
        cout << "That's not a valid number\n";
        cout << "This program will terminate on any keypress!";
        _getch();
        exit(1);
    }
    total_pence = calcNum(pence, shilling, pound);
    deci_pound = calcNew(dec_pound, total_pence);
    cout << (5, "\n");
    cout << "The total amount in decimal pounds is: ";
    cout << setprecision(2) << "\x9c" << deci_pound;
    _getch();
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
国际货币(国际英镑、国际先令、国际便士)
{
便士=英镑*240+先令*12+便士;
归还便士;
}
整数新币(整数总便士,双倍12英镑)
{
12英镑=总便士/240;
退回12英镑;
}
int main()
{
一便士;
国际先令;
国际磅;
旧磅常数=240;
旧先令常数=12;
两倍总便士;
双倍重量=0;
双分贝;
重量>磅;
cout你的
calcNew(…)
函数返回一个int,让它返回一个
double
。现在它转换为
int
,这涉及到去除小数


在您的代码中,
dec\u pound
设置为零,您的
deci\u pound=calcNew(dec\u pound,total\u pence)
,它将0除以240=0。

调用这两个函数时参数的顺序是错误的。您的函数声明和实现如下:

int calcNum(int pound, int shilling, int pence);
int calcNew(int total_pence, double dec_pound);
然后你这样称呼他们:

total_pence = calcNum(pence, shilling, pound);
deci_pound = calcNew(dec_pound, total_pence);

试着在calcNew中除法之前将[code>dec_pound
转换为double,我想这可能是整数除法的问题。“新系统”?他们在1971年切换。不要在货币计算中使用[code>double类型,伙计。否则你会玩得不开心。我不太清楚你的意思。我把“int”改为“double”在函数名之前,但不幸的是,这仍然不起作用。也许您的意思是将
pound
而不是
dec\u pound
传递到
calcNew(…)