setw(n)和对齐不能按我需要的方式工作 - C ++

时间:2018-03-01 01:13:11

标签: c++ setw

这是我的上下文代码。这是我的第二个家庭作业编程课程介绍,我已经使用了我们在这个作业中学到的一切。我不被允许使用任何我没学过的东西。

我关注的部分是最底部的输出(注释为信息输出)。目前,我正在努力使所有东西完全正确对齐(右栏中的最后一个字母或数字必须彼此对齐,就像我从右边键入它们一样。

除了患者的姓名,房间类型和在医院度过的天数之外的所有事情都正确对齐,小数点对齐以及所有内容。将setw(10)更改为更大的东西没有任何好处(例如:我将它们全部设置为setw(40)并且它仍然没有正确对齐任何东西。有任何想法吗?

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

//constants
const float PRIVATE = 125.00;
const float SEMI = 95.00;
const float WARD = 75.00;
const float TV = 3.50;
const float PHONE = 1.75;

int main()
{   //local variables
    string fname, lname, roomType, tvAccess, phoneAccess;
    int id, days;
    float roomBill, roomPrice, tvCharge, phoneCharge;
    bool error = false;

    //data collection/output
    cout << "Welcome to the hospital self-service program. Your bill will be calculated here.\n\n" << endl;
    cout << "Please enter your first and last name: ";
    cin >> fname;
    cin >> lname;
    cout << fname << " " << lname << ", please enter the four digit identification number found on your hospital wristband: ";
    cin >> id;
    cout << "Please enter the number of days spent in the hospital: ";
    cin >> days;
    cout << "\nEnter the type of room you stayed in: \nEnter P for room type: Private \nEnter S for room type: Semi-Private\nEnter W for room type: Ward \n" << endl;
    cin >> roomType;
    cout << "\nDid your room come with access to Television? Y/N: ";
    cin >> tvAccess;
    cout << "Did your room come with access to a Telephone? Y/N: ";
    cin >> phoneAccess;

    //if and elses
    if (roomType == "P" || roomType == "p")
    {
        error = false;
        roomType = "Private Room";
        roomPrice = PRIVATE;
    }
    else if (roomType == "S" || roomType == "s")
    {
        error = false;
        roomType = "Semi-Private Room";
        roomPrice = SEMI;

    }
    else if (roomType == "W" || roomType == "w")
    {
        error = false;
        roomType = "Ward Room";
        roomPrice = WARD;

    }
    else
    {
        cout << "Room type not valid. Exit the program and try again." << endl;
        error = true;
    }
    if (tvAccess == "Y" || tvAccess == "y")
        tvCharge = TV;
    else
        tvCharge = 0;

    if (phoneAccess == "Y" || phoneAccess == "y")
        phoneCharge = PHONE;
    else
        phoneCharge = 0;


    //information output
    cout << fixed << setprecision(2) << showpoint << endl;
    cout << setw(24) << left << "\n\nPatient Full Name: " << setw(10) << right << fname << " " << lname << endl;
    cout << setw(24) << left << "Identification Number: " << setw(10) << right << id << endl;
    cout << setw(24) << left << "Days spent in hospital: " << setw(10) << right << days << " day(s)" <<  endl;
    cout << setw(24) << left << "Room Type: " << setw(10) << right << roomType << endl;
    cout << setw(24) << left << "Room Charge: " << setw(10) << right << roomPrice * days << endl;
    cout << setw(24) << left << "Television Charge: " << setw(10) << right << tvCharge * days << endl;
    cout << setw(24) << left << "Telephone Charge: " << setw(10) << right << phoneCharge * days << endl;
    cout << setw(24) << left << "Total Charge: " << setw(10) << right << days * (phoneCharge + tvCharge + roomPrice) << endl;

    system("pause");
    return 0;
}

This is an image澄清。

1 个答案:

答案 0 :(得分:2)

原因是,它们的格式不正确,是<{p>}中的setw

setw(10) << right << fname << " " << lname << endl;

setw(10) << right << days << " day(s)" <<  endl;

仅适用于第一个变量,即fnamedays。其他一切都只是附加。在使用setw之前,您需要先连接这些字符串。