C++ 如果字段中的字符数不同,则输出时分隔符会丢失
我的格式有问题。我的程序收集关于学生的信息,然后输出用户输入的信息。 结论应该精确地出现在这些列中(我指的是制表法和“| |”)。 问题是,对于不同大小的字段,这些分隔符“| |”对我来说会丢失,而且总体而言,所有这些看起来都很难看。可以做些什么,使所有的东西都能漂亮地展示出来。即使某些字段中的字符数不匹配 如何在Pascal上解决此问题,例如:C++ 如果字段中的字符数不同,则输出时分隔符会丢失,c++,C++,我的格式有问题。我的程序收集关于学生的信息,然后输出用户输入的信息。 结论应该精确地出现在这些列中(我指的是制表法和“| |”)。 问题是,对于不同大小的字段,这些分隔符“| |”对我来说会丢失,而且总体而言,所有这些看起来都很难看。可以做些什么,使所有的东西都能漂亮地展示出来。即使某些字段中的字符数不匹配 如何在Pascal上解决此问题,例如: write(first.surname:8:6); 输出: Information you entered >
write(first.surname:8:6);
输出:
Information you entered >
Surname < Trump || Obama |
|
Group < rz-1 || rpds-5 |
|
Course < 2 || 3 |
|
Physics < 3 || 4 |
|
Programming < 5 || 4 |
|
Math < 3 || 3 |
|
Name< Donald || Barack |
|
Patronymic < Donalldd || Bakkaa |
|
Average < 3.66667 || 3.66667 |
|
您输入的信息>
姓
代码:
#包括
使用名称空间std;
浮标;
结构学生
{
查氏[21];
char组[21];
国际课程;
国际物理学;
整数编程;
整数数学;
浮标;
//学生信息
char WorkersName[21];//学生姓名
char JobTitle[21];//学生的中间名
};
int main()
{
setlocale(0,“”);
sr_标记=0;
首先是物理;
cout>first.programming;
首先是数学;
cout>first.WorkersName;
cout>first.JobTitle;
第二道菜;
cout>第二->物理;
cout>second->programming;
cout>second->math;
cout>second->WorkersName;
cout>second->JobTitle;
数学能力;
sr_mark+=第二次->编程;
sr_mark+=第二->物理;
sr_标记/=3;
第二->标记=sr\U标记;
sr_标记=0;
cout这是一项复杂的任务,没有神奇的解决方案:您需要测量字段的大小(以字符为单位),然后相应地添加空格,等等。
更不用说可能存在的utf-8问题(以字节为单位的字符串大小可能与以字符为单位的大小不同)
您可以使用std::setw()
,但这并不能解决所有问题
你最好使用图书馆。
如果你有C++17可用,似乎是一个相当整洁的解决方案。
对你的要求来说,可能有点过分了
(免责声明:我自己没有使用过。)选项卡“\t”很可能不起作用。您应该使用头文件中定义的std::setw
。任何像样的书、教程或类都应该提到。其中一些可以用于列格式设置。
#include <iostream>
using namespace std;
float sr_mark;
struct student
{
char surname[21];
char group[21];
int course;
int physics;
int programming;
int math;
float mark;
// Student Information
char WorkersName[21]; // student name
char JobTitle[21]; // middle name of student
};
int main()
{
setlocale(0, "");
sr_mark = 0;
cout << "\n";
student first;
cout << "\t" << "Enter information about the first student < " << endl;
cout << "\n";
cout << " Enter surname > ";
cin >> first.surname;
cout << "\n";
cout << " Enter group > ";
cin >> first.group;
cout << "\n";
cout << " Enter course > ";
cin >> first.course;
cout << "\n";
cout << " Enter physics grade > ";
cin >> first.physics;
cout << "\n";
cout << " Enter a programming grade > ";
cin >> first.programming;
cout << "\n";
cout << " Enter math grade > ";
cin >> first.math;
cout << "\n";
cout << " Enter student name > ";
cin >> first.WorkersName;
cout << "\n";
cout << " Enter the middle name of the student > ";
cin >> first.JobTitle;
cout << "\n";
sr_mark += first.math;
sr_mark += first.programming;
sr_mark += first.physics;
sr_mark /= 3;
first.mark = sr_mark;
sr_mark = 0;
cout << "\n";
cout << "\t" << "Enter data about the second student < " << endl;
cout << "\n";
student *second = new student;
cout << " Enter surname > ";
cin >> second -> surname;
cout << "\n";
cout << " Enter group > ";
cin >> second -> group;
cout << "\n";
cout << " Enter course > ";
cin >> second -> course;
cout << "\n";
cout << " Enter physics grade > ";
cin >> second -> physics;
cout << "\n";
cout << " Enter a programming grade > ";
cin >> second -> programming;
cout << "\n";
cout << " Enter math grade > ";
cin >> second -> math;
cout << "\n";
cout << " Enter student name > ";
cin >> second -> WorkersName;
cout << "\n";
cout << " Enter the middle name of the student > ";
cin >> second -> JobTitle;
cout << "\n";
sr_mark += second -> math;
sr_mark += second -> programming;
sr_mark += second -> physics;
sr_mark /= 3;
second -> mark = sr_mark;
sr_mark = 0;
cout << "\n";
cout << "Information you entered >" << endl;
cout << "\n";
cout << " Surname < " << "\t" <<first.surname << "\t\t\t\t" << "||" << "\t\t" << second -> surname << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Group < " << "\t" <<first.group << "\t\t\t\t" << "||" <<"\t\t" << second -> group << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Course < " << "\t" <<first.course << "\t\t\t\t" << "||" <<"\t\t" << second -> course << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Physics < " << "\t" <<first.physics << "\t\t\t\t" << "||" <<"\t\t" << second -> physics << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Programming < " << "\t" <<first.programming << "\t\t\t\t" << "||" <<"\t\t" << second -> programming << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Math < " << "\t" <<first.math << "\t\t\t\t" << "||" <<"\t\t" << second -> math << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Name<" << "\t" <<first.WorkersName << "\t\t\t\t" << "||" <<"\t\t" << second -> WorkersName << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Patronymic < " << "\t" <<first.JobTitle << "\t\t\t\t" << "||" <<"\t\t" << second -> JobTitle << "\t\t\t\t" << "||" << "\t\t\n";
cout << " Average < " << "\t" <<first.mark << "\t\t\t\t" << "||" <<"\t\t" << second -> mark << "\t\t\t\t" << "||" << "\t\t\n";
system("pause");
}