简体   繁体   English

setw无法正常工作

[英]setw not working properly

Im writing a loan amortization program but when I use "setw" to set up my columns its putting "month" and "Current Balance" together and not spacing the others like I want. 我正在编写一个贷款摊销程序,但是当我使用“ setw”设置我的列时,它会将“ month”和“ Current Balance”放在一起,而没有像我想要的那样间隔其他人。

I included a picture below of the output on the terminal. 我在终端的输出下方包括了一张图片。

cout << "Month" // sets up columns
     << setw(15) << "Current Balance"
     << setw(15) << "Interest"
     << setw(15) << "Payment"
     << setw(15) << "New Balance \n" << endl;


int month_count = 1;
while( month_count <= number_of_months) // Loops while calculating the monthly interest and new balances produced after payments.
    {
    cout << month_count
         << setw(15) << loan_amount;

    double interest = loan_amount * monthly_rate;

    cout << setw(15) << interest
         << setw(15) << payment;

    loan_amount = loan_amount + interest - payment;

    cout << setw(15) << loan_amount << endl;

    month_count++;
    }

OUTPUT 输出值

这是其对齐方式

I'd suggest a few things: 我建议一些事情:

1st: Increase your width to something slightly longer than the maximum width of your column names. 第一:将宽度增加到比列名称的最大宽度稍长的长度。 In this case you have "Current Balance" which is 15 characters long, so if you use 15 as your field width, you won't get spacing around that column. 在这种情况下,您的“当前余额”为15个字符长,因此,如果您使用15作为字段宽度,则该列之间将不会有间距。

2nd: Use std::setw together with std::left for your first column to space our that column too. 第二:在第一列中使用std::setwstd::leftstd::setw该列。 ( std::left will left-justify the first column) std::left将使第一列左对齐)

3rd: Use std::setw together with std::right for your value columns to right align the values, and use a combination of std::fixed and std::setprecision(2) to always print 2 decimal places for your dollar/cent amounts, which makes it easier to read 第三:将std::setwstd::right结合使用以使值列右对齐,并结合使用std::fixedstd::setprecision(2)始终为您的美元/美元打印2个小数位美分的金额,使阅读更容易

Here is an example: 这是一个例子:

#include <iostream>
#include <iomanip>

using namespace std;

int number_of_months = 3;
double loan_amount = 50;
double monthly_rate = 2.3;
double payment = 10;

int main()
{
    cout << setw(7)  << std::left << "Month" // sets up columns
         << setw(17) << std::right << "Current Balance"
         << setw(17) << std::right << "Interest"
         << setw(17) << std::right << "Payment"
         << setw(17) << std::right << "New Balance" << '\n' << endl;


    int month_count = 1;
    while( month_count <= number_of_months) // Loops while calculating the monthly interest and new balances produced after payments.
    {
        cout << setw(7) << std::left << month_count
             << setw(17) << std::right << std::setprecision(2) << std::fixed << loan_amount;

        double interest = loan_amount * monthly_rate;

        cout << setw(17) << std::right << std::setprecision(2) << std::fixed << interest
             << setw(17) << std::right << std::setprecision(2) << std::fixed << payment;

        loan_amount = loan_amount + interest - payment;

        cout << setw(17) << std::right << std::setprecision(2) << std::fixed << loan_amount << endl;

        month_count++;
    }

    return 0;
}

Output: 输出:

Month    Current Balance         Interest          Payment      New Balance

1                  50.00           115.00            10.00           155.00
2                 155.00           356.50            10.00           501.50
3                 501.50          1153.45            10.00          1644.95

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM