如何添加货币面额c ++

时间:2015-02-05 20:44:43

标签: c++ currency

非常新的程序员,我很难找到设置此问题的方法。我有钱面额,一美元到一百美元。用户必须输入让我们说例如4美元和5美元,并获得29美元的总和。我不知所措,我一直在尝试这里是我的...

{   // dollar values 

    int n = std::numeric_limits<int>::max();
    int b = std::numeric_limits<int>::max();
    int dollarOne = 1;
    int dollarTwo = 2;
    int sum1;                                               // defines sum 


    cin >> n >> dollarOne; 
    sum1 = ((n*dollarOne)+(b*dollarTwo);                                // sum function 


    cout << sum1 << endl;                               // displays total amount


    system("pause");
    return 0;
}

        int dollarAm1;         int dollarAm2;         int sum;         cin&gt;&gt; dollarAm1;         cin&gt;&gt; dollarAm2;         sum =((dollarAm1 * 1)+(dollarAm2 * 2));

    cout << sum << endl; 

3 个答案:

答案 0 :(得分:1)

你错过了一个括号

sum1 = ((n*dollarOne)+(b*dollarTwo);

接下来,您应该使用cin.get()来结束您的计划,而不是system("PAUSE")。这在处理速度方面更有效。此外,您应该只需int n, b;来初始化变量。

你应该阅读这样的输入:

std::cout << "Enter number of 1-dollar bills, and press \"Enter\". Next, enter number of 5-dollar bills:" << std::endl;

cin >> dollarOne;
cin >> dollarTwo;

答案 1 :(得分:1)

int n = 0;
int b = 0;
int dollarOne = 1;
int dollarFive = 5;
int sum1; // defines sum


std::cin >> n;
std::cin >> b;

sum1 = (n*dollarOne)+(b*dollarFive); // sum function


std::cout << sum1 << std::endl; // displays total amount


system("pause");
return 0;

答案 2 :(得分:1)

尝试更类似的内容(为简洁起见,删除了错误处理):

{
    int n;
    int sum1 = 0;

    cout << "How many $1 bills: ";
    cin >> n;
    sum1 += (n*1);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $5 bills: ";
    cin >> n;
    sum1 += (n*5);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $10 bills: ";
    cin >> n;
    sum1 += (n*10);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $20 bills: ";
    cin >> n;
    sum1 += (n*20);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $50 bills: ";
    cin >> n;
    sum1 += (n*50);

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    cout << "How many $100 bills: ";
    cin >> n;
    sum1 += (n*100);

    cout << "Total: $" << sum1 << endl;

    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cin.get();

    return 0;
}