数组中的元素乘以c ++不可用?

时间:2018-04-25 21:30:57

标签: c++ visual-studio

我试图在c ++中创建一个简单的程序,它采用购买的物品的价格和数量,将其存储在数组中,然后以表格格式输出每个项目的总数。但是,当我将代码中的数字相乘时,我会得到完全奇怪的答案!有人可以告诉我发生了什么事吗?

代码:

#include <iostream>

using namespace std;

int main(int argc, _TCHAR* argv[]) {
    float price[4], tot[4];
    int amt[4];
    cout << "Please enter the price and amount of 4 items:\n";
    for (int i = 0; i<4; i++) {
        cout << "Price of item " << i + 1 << ": ";
        cin >> price[i];
        cout << "Amount of item " << i + 1 << ": ";
        cin >> amt[i];
        if (price[i] <= 0 || amt[i] <= 0) {
            cout << "Invalid Input Entry!\n";
            break;
        }
        tot[i] = price[i] * amt[i]; // I can't really see how I could have messed this up...
    }
    cout << "Total\t\tPrice\t\tAmount\n";
    cout << "-----\t\t-----\t\t------\n";
    for (int i = 0; i < 4; i++) {
        cout << "$" << fixed << cout.precision(2) << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
    }
    system("pause");
    return 0;
}

输出:

program output

1 个答案:

答案 0 :(得分:5)

问题是您输出的返回值为http://a.cloudxlab.com:8088/proxy/application_1524162637175_1044/(在此情况下返回先前的精度6,然后在每个总价格前返回2

您需要:

  • 未将cout.precision()的返回值传递给operator<<

    cout << "$" << fixed;
    cout.precision(2);
    cout << tot[i] << ...
    

    或者,在进入循环之前调用precision()一次:

    cout.precision(2);
    for (int i = 0; i < 4; i++) {
        cout << "$" << fixed << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
    }
    
  • 使用cout.precision(2)流操作符,而不是直接调用cout.precision()

    #include <iomanip>
    
    for (int i = 0; i < 4; i++) {
        cout << "$" << fixed << setprecision(2) << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
    }
    

    #include <iomanip>
    
    cout << setprecision(2);
    for (int i = 0; i < 4; i++) {
        cout << "$" << fixed << tot[i] << "\t\t$" << price[i] << "\t\t" << amt[i] << endl;
    }
    

另外,您不应使用\t字符来控制表格的格式。请改用std::setw()std::left等流操作符:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdlib>    
using namespace std;

const int maxItems = 4;

string moneyStr(float amount)
{
    ostringstream oss;

    // in C++11 and later, you can replace this with std::put_money() instead:
    // http://en.cppreference.com/w/cpp/io/manip/put_money
    //
    // oss << showbase << put_money(amount);
    oss << "$" << fixed << setprecision(2) << amount;

    return oss.str();
}

int main(int argc, _TCHAR* argv[])
{
    float price[maxItems], tot[maxItems];
    int amt[maxItems];
    int cnt = 0;

    cout << "Please enter the price and amount of " << maxItems << " items:" << endl;
    for (int i = 0; i < maxItems; ++i)
    {
        cout << "Price of item " << i + 1 << ": ";
        cin >> price[i];
        cout << "Amount of item " << i + 1 << ": ";
        cin >> amt[i];
        if (price[i] <= 0 || amt[i] <= 0) {
            cout << "Invalid Input Entry!" << endl;
            break;
        }
        tot[i] = price[i] * amt[i];
        ++cnt;
    }

    cout << left << setfill(' ');
    cout << setw(16) << "Total" << setw(16) << "Price" << setw(16) << "Amount" << endl;
    cout << setw(16) << "-----" << setw(16) << "-----" << setw(16) << "------" << endl;
    for (int i = 0; i < cnt; i++) {
        cout << setw(16) << moneyStr(tot[i]) << setw(16) << moneyStr(price[i]) << setw(16) << amt[i] << endl;
    }

    system("pause");
    return 0;
}

std::setprecision()