【问题标题】:setw(n) and alignment not working the way I need it to - C++setw(n) 和对齐没有按我需要的方式工作 - C++
【发布时间】:2018-03-01 01:13:10
【问题描述】:

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

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

除了患者姓名、房间类型和在医院度过的天数之外的所有内容都正确对齐,小数对齐等等。将 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;
}

澄清一下。

【问题讨论】:

  • 请给我们您的预期输出和实际输出。
  • 我不知道该怎么做,我认为我的解释已经足够了。让我看看我能不能给它画一张照片。
  • 你可以像这样捕获输出:./aout arg1 >> result.txt
  • 对于初学者,你应该把cout &lt;&lt; setw(24) &lt;&lt; left &lt;&lt; "\n\n前面的\n\n去掉,然后在setw之前写下来。 cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; setw(24) &lt;&lt; ...
  • 安德斯 K!!!你是男人,这就是问题所在!!!我爱你,伙计。

标签: c++ setw


【解决方案1】:

它们的格式不正确的原因是,setw in

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

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

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

【讨论】:

  • 所以我认为这就是你的意思,我在代码末尾添加了这个。字符串名称 = fname + ' ' + lname;现在我将名字、姓氏和一个空格都存储在一个字符串中。它仍然没有按预期工作。现在所有内容都右对齐了,除了名称,现在左边有几个空格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多