Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

align outputs perfectly in c++

  1. User

    align outputs perfectly in c++

  2. Assistant

    To align outputs perfectly in C++, you can make use of the setw() and left manipulators from the <iomanip> library. Here's an example:

    #include <iostream>
    #include <iomanip>
    
    int main() {
        std::cout << std::setw(10) << std::left << "Name";
        std::cout << std::setw(10) << std::left << "Age";
        std::cout << std::setw(10) << std::left << "City" << std::endl;
    
        std::cout << std::setw(10) << std::left << "John";
        std::cout << std::setw(10) << std::left << "25";
        std::cout << std::setw(10) << std::left << "New York" << std::endl;
    
        std::cout << std::setw(10) << std::left << "Jane";
        std::cout << std::setw(10) << std::left << "30";
        std::cout << std::setw(10) << std::left << "London" << std::endl;
    
        return 0;
    }
    

    Output:

    Name      Age       City      
    John      25        New York  
    Jane      30        London    
    

    In the above example, setw(10) sets the field width to 10 characters for each output, and left aligns the output to the left within that field width.