[英]I am trying to polish my output using (setw) but if the value that i enter differ in range it does not work properly
我正在尝试使用setw(4)修复我的输出。我尝试了1到10之间的一个数字范围,但没有一个让我获得想要的输出。
void Students(int& size_public)
{
cout << "Input the number of information to be entered: ";
cin >> size_public;
cout << endl << endl;
// Reseting the screen
system("cls");
for (int i = 0; i < size_public; i++)
{
cout << "________________________Enter the following information for the data to be stored________________________\n" << endl;
cout << "Name: ";
cin >> employees[i].name_public;
cout << "Age: ";
cin >> employees[i].age_public;
cout << "ID.No: ";
cin >> employees[i].ID_No_publice;
cout << endl;
system("cls");
}
}
void Students(Employee employees[arraysize], int& size_public)
{
for (int i = 0; i < size_public; i++)
{
if (i == 0)
{
cout << setw(10) << "Name\t" << "Age\t" << "ID.No" << endl << endl;
}
cout << setw(10) << employees[i].name_public << '\t'
<< employees[i].age_public << '\t'
<< employees[i].ID_No_publice << endl;
}
}
};
在程序的后面,它被称为
if (selection == 2)
{
student.Students(size);
cout << "1. Leave\n";
cout << "2. Showinformation\n";
cout << "\n________________________________________________\n";
cout << "Enter your choice: ";
cin >> choice;
cout << "\n________________________________________________\n";
if (choice == 1)
{
cout << endl;
}
else if (choice == 2)
{
student.Students(student.employees, size);
}
else if (choice != 1 && choice != 2)
{
cout << "choice is not found\n";
}
}
这就是我在说的
Name Age ID.No
fge 33 345674
sdfgfd 34 23
dfghjkjhg 354 54345
Space behind the names is what I am trying to avoid
This is the output i receive when i use setw(10)
and this is what happen when the range of input changes
Name Age ID.No
afjghbslkk;jfl 2 3
fg 3 5.67654e+06
3 34543 543
if you look at fg in name it has a space behind it and I am trying to avoid it
My desired output is something like
Name age ID.No
dfghsgffgdf 44553 4564564
ajkghjkgh 444 465454
ff 4 46
我不知道是否有可能,但是如果我错过任何事情,请告诉我,我将更新代码。
例
if (i == 0)
cout << left << setw(10) << "Name" << setw(10) << "Age" << setw(10) << "ID.No" << endl << endl;
cout << left << setw(10) << employees[i].name_public
<< setw(10) << employees[i].age_public
<< setw(10) << employees[i].ID_No_publice << endl;
看看这是否是您所需要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.