非常新的程序员,我很难找到解决此问题的方法。我有货币单位,一美元到一百美元。用户必须输入例如说4美元1美元和5美元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;
        整数和
        cin >> dollarAm1;
        cin >> dollarAm2;
        总和=((dollarAm1 * 1)+(dollarAm2 * 2));

    cout << sum << endl;

最佳答案

尝试使用类似以下的方法(为简洁起见,删除了错误处理):

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

关于c++ - 如何添加货币面额C++,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28353655/

10-09 09:49