I am having trouble understanding the purpose of setw(n) function in C++

199 Views Asked by At

I have been studying C++ from a textbook and in my opinion their explanations of the purpose of the setw() functions have been poor. Simply put, I do not understand the purpose of these functions, and online explanations have not been much help either. I have read that it "Sets the field width to be used on output operations" or "sets the width parameter of the stream out or in to exactly n" but what on earth does that mean? Before you answer this question, I have two codes, A and B which better illustrate my confusion.

Code A: (sample code from the textbook):

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

using namespace std;

int main() 
{
   string label; 
   double price;  
  
   cout << "\nPlease enter an article label: ";
 
   cin >> setw(16);        
   cin >> label;

   cin.sync();    
   cin.clear();   

   cout << "\nEnter the price of the article: ";
   cin >> price;           
 
   cout << fixed << setprecision(2)
        << "\nArticle:"
        << "\n  Label:  " << label
        << "\n  Price:  " << price << endl;
  
return 0;

}

example input: Please enter an article label: computer Enter the price of the article: 75

output:

Article: Label: computer Price: 75.00

Code B: (nearly the exact same code as A but with three of the lines commented out)

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

using namespace std;

int main() 
{
   string label; 
   double price;  
  
   cout << "\nPlease enter an article label: ";
 
   //cin >> setw(16);        
   cin >> label;

   //cin.sync();    
   //cin.clear();   

   cout << "\nEnter the price of the article: ";
   cin >> price;           
 
   cout << fixed << setprecision(2)
        << "\nArticle:"
        << "\n  Label:  " << label
        << "\n  Price:  " << price << endl;
  
return 0;

}

example input: Please enter an article label: computer Enter the price of the article: 75

output: Article: Label: computer Price: 75.00

In code B I commented out the setw(16), cin.sync(), and cin.clear() lines, but I do not see any changes in the output regardless of if the lines are commented out or not. So my question is, what is the purpose of these lines? What does code A gain by having these extra lines compared to code B which does not? Is there some input that will result in the outputs of A and B to be different, or is the difference something that cannot be seen with the eyes? Furthermore, what is the purpose of the integer n in the sets(n) line. I have changed this number to different values such as 16, 78, 100, 10000, etc but that does not appear to change the outputs of the two codes either. Thanks.

1

There are 1 best solutions below

0
On

Let's take this code:

#include <iostream>
#include <iomanip>

int main() {
    double value = 1234567890.321654987;
    
    std::cout << value << '\n';
    
    std::cout << std::setw(20) << value << '\n';
    std::cout << std::setw(2) << value << '\n';
    
    
    std::cout << std::setprecision(2) << value << '\n';
    std::cout << std::setprecision(20) << value << '\n';
}

It prints:

1.23457e+09
         1.23457e+09
1.23457e+09
1.2e+09
1234567890.321655035

The width is the min number of characters: white spaces may be added if needed.

The precision is the number of digits. The first print uses the default precision (6 apparently).