简体   繁体   中英

I am trying to polish my output using (setw) but if the value that i enter differ in range it does not work properly

I am trying to fix my output using setw(4).I have tried different a range of number between 1 to 10 but none get me the desired output.

    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

I do not know if it is possible but if I am missing any thing just let me know and i will update the code.

Example

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;

See if this is what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM