gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 01:36:31 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

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

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

关于c++ - setw(n) 和对齐不按我需要的方式工作 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49041179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com