c++ - 似乎无法精确排列小数

标签 c++ input io iomanip

我有一个简单的程序,它询问电影的名称,以及 set1 和 set2 的售票数量。然后它对这些值进行一些计算并显示结果。我遇到的唯一问题是我似乎无法按照我希望的方式排列十进制值。

控制台中的最后三行输出应始终如下所示(美元符号和小数点对齐):image .但是当我为 set1 门票输入 1,为 set2 门票输入 0 时,反之亦然,它看起来像这样:image .关于如何使输出始终像第一个屏幕截图那样很好地排列的任何想法?提前致谢。

    #include <iostream>
    #include <string>
    #include <iomanip>

    using namespace std;

    int main() {

    string film = "";

    int set1 = 0;
    int set2 = 0;
    // the cinema always takes this 20% cut.
    const double CINEMA_FEE = 0.20;
    double profit = 0;
    double profitMinusFee = 0;
    double paidToMovieMaker = 0;

    cout << "What is the name of the film: ";
    getline(cin, film);

    cout << "How many tickets for set1 sold: ";
    cin >> set1;

    cout << "How many tickets for set2 sold: ";
    cin >> set2;

    cout << "Film Name:" << setw(20) << "\"" << film << "\"" << endl;
    cout << "Set1 tickets sold:" << setw(16) << set1 << endl;
    cout << "Set2 tickets sold:" << setw(16) << set2 << endl;

    set1 *= 10;
    set2 *= 6;
    profit = set1 + set2;
    profitMinusFee = profit * CINEMA_FEE;
    paidToMovieMaker = profit - profitMinusFee;
    // needs to always show two decimal points and fixed
    cout << setprecision(2) << fixed;
    cout << "The total monetary profit:" << setw(5) << "$ " << profit << endl;
    cout << "The net monetary profit:" << setw(7) << "$ " << profitMinusFee << endl;
    cout << "Total paid to movie maker:" << setw(5) << "$ " << paidToMovieMaker << endl;

    return 0;
    }

最佳答案

我认为您的问题是您在美元符号而不是数字上设置宽度。

这似乎为我解决了这个问题:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main() {

    string film = "";

    int set1 = 0;
    int set2 = 0;
    // the cinema always takes this 20% cut.
    const double CINEMA_FEE = 0.20;
    double profit = 0;
    double profitMinusFee = 0;
    double paidToMovieMaker = 0;

    cout << "What is the name of the film: ";
    getline(cin, film);

    cout << "How many tickets for set1 sold: ";
    cin >> set1;

    cout << "How many tickets for set2 sold: ";
    cin >> set2;

    cout << "Film Name:" << setw(20) << "\"" << film << "\"" << endl;
    cout << "Set1 tickets sold:" << setw(16) << set1 << endl;
    cout << "Set2 tickets sold:" << setw(16) << set2 << endl;

    set1 *= 10;
    set2 *= 6;
    profit = set1 + set2;
    profitMinusFee = profit * CINEMA_FEE;
    paidToMovieMaker = profit - profitMinusFee;
    // needs to always show two decimal points and fixed
    cout << setprecision(2) << fixed;
    cout << "The total monetary profit: $ " << setw(10) << profit << endl;
    cout << "The net monetary profit  : $ " << setw(10) << profitMinusFee << endl;
    cout << "Total paid to movie maker: $ " << setw(10) << paidToMovieMaker << endl;

    return 0;
}

记住 setw() 会影响 它之后的东西。

关于c++ - 似乎无法精确排列小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554067/

相关文章:

c++ - 错误 : 'myfile' was not declared in this scope

c++ - 在 C++ 应用程序的使用中需要对数组、 vector 和映射进行说明

javascript - 理解输入的智能正则表达式

c++ - 线程(C++ 中的并行计算)是否与优化级别 (gcc) 不兼容?

c++ - 绑定(bind).gyp : How to use "copies" section to copy files in multiple location

css - 隐藏/样式化文件输入在 Firefox 中有效,但在 Chrome 中无效

java - Java 中通过标准输入/输出进行通信

c++ - boost::lexical_cast 可以将字符串中的十六进制转换为整数吗?

c# - MFC 应用程序中的 .NET Activex 控件访问冲突

javascript - 检查输入字段的值