gpt4 book ai didi

c++ - 我正在尝试使用 (setw) 来完善我的输出,但是如果我输入的值在范围内不同,它就无法正常工作

转载 作者:行者123 更新时间:2023-11-28 04:15:02 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}
}

};

Later in the program it is called

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";
}

}

This is what I am talking about

 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;

看看这是否是您需要的。

关于c++ - 我正在尝试使用 (setw) 来完善我的输出,但是如果我输入的值在范围内不同,它就无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56864301/

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