-1

been trying to get this to align, and every source has told me to use left and setw, but no matter how I format it I can't seem to get it to work. Anyone have any ideas?

#include <iostream>
#include <string>
#include <iomanip> using namespace std;

int main() {   string name[5] = {"able", "beehive", "cereal", "pub", "deck"};   string lastName[5] = {"massive", "josh", "open", "nab", "itch"};   float score[5] = {12, 213, 124, 123, 55};   char grade[5] = {'g', 'a', 's', 'p', 'e'};   int counter = 5;    for (int i = 0; i < counter; i++){
    cout << left
    << name[i] << " " << setw(15) << left
    << lastName[i] << setw(15) << left
    << score[i] << setw(20)
    << grade[i];     cout << endl;    } }

This is the output:

able massive        12             g                   
beehive josh           213            a                   
cereal open           124            s                   
pub nab            123            p                   
deck itch           55             e
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185

2 Answers2

2

setw sets the width of the next output. It does not retroactively change how previous output is formatted. Instead of ... << someoutput << setw(width) you want ... << setw(width) << someoutput:

#include <iostream>
#include <string>
#include <iomanip> 
using namespace std;

int main() {   
    string name[5] = {"able", "beehive", "cereal", "pub", "deck"};   
    string lastName[5] = {"massive", "josh", "open", "nab", "itch"};  
    float score[5] = {12, 213, 124, 123, 55};   
    char grade[5] = {'g', 'a', 's', 'p', 'e'};   
    int counter = 5;   
    for (int i = 0; i < counter; i++){
        cout << left <<  " " << setw(15) << left << name[i] 
             <<  setw(15) << left << lastName[i] 
             <<  setw(20) << score[i]
             << grade[i];   
        cout << endl;    
   } 
}

Live:

 able           massive        12                  g
 beehive        josh           213                 a
 cereal         open           124                 s
 pub            nab            123                 p
 deck           itch           55                  e
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I'm so confused by all of this, but maybe it's cause I'm incredibly sleep deprived, thank you for clearing this up. – ItsYaBFF I Jan 18 '22 at 11:07
0

Here is your code with a bit of refactoring:

#include <iostream>
#include <iomanip>
#include <string>
#include <array>


int main( )
{
    // use std::array instead of antiquated raw arrays, it does
    // the same job but more conveniently

    // also use value initialization like `{ }` instead of this `= { }`

    std::array<std::string, 5> name { "able", "beehive", "cereal", "pub", "deck" };
    std::array<std::string,5> lastName { "massive", "josh", "open", "nab", "itch" };
    std::array<float, 5> score { 12, 213, 124, 123, 55 };
    std::array<char, 5> grade { 'g', 'a', 's', 'p', 'e' };
    
    constexpr std::size_t counter { name.size( ) }; // `counter` can be a
                                                    // constant expression so
                                                    // declare it constexpr

    for ( std::size_t idx { }; idx < counter; ++idx ) // std::size_t for the
    {                                                 // loop counters
        std::cout << std::left << ' '
        << std::setw(15) << std::left << name[idx]
        << std::setw(15) << std::left << lastName[idx]
        << std::setw(20) << std::left << score[idx]
        << std::setw(1) << std::left << grade[idx];

        std::cout << '\n';
    } 
}

Output:

 able           massive        12                  g
 beehive        josh           213                 a
 cereal         open           124                 s
 pub            nab            123                 p
 deck           itch           55                  e

digito_evo
  • 3,216
  • 2
  • 14
  • 42