why does \n vs endl work differntly with setw output

Mar 23, 2015 at 1:30am
This code works perfectly but if you were to use the \n in the setw it will mess up the output format anyone have a good explanation why?


heres an example if you switch out these two codes:

cout << left << setw(10) << "Names" << setw(10) << "Status" << setw(10) << "Hours" << setw(10) << "Sales" << setw(10) << "Wages" << setw(10) << "Pay\n\n";





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


string name,type,hours,sales,wages,pay;


int main()
{
cout << "name:\n";
cin >> name;
cout << endl;
cout << "type:\n";
cin >> type;
cout << endl;
cout << "hours:\n";
cin >> hours;
cout << endl;
cout << "sales:\n";
cin >> sales;
cout << endl;
cout << "wages: \n";
cin >> wages;
cout << endl;
cout << "pay:\n";
cin >> pay;

cout << "Employee Pay Report \n\n";
cout << left << setw(10) << "Names" << setw(10) << "Status" << setw(10) << "Hours" << setw(10) << "Sales" << setw(10) << "Wages" << setw(10) << "Pay" << endl << endl;
cout << left << setw(10) << name << setw(10) << type << setw(10) << hours << setw(10) << sales << setw(10) << wages << setw(10) << pay << endl << endl << endl;

}
Mar 23, 2015 at 3:18am
That's because '\n' is a character. It's counted towards the total length of the string you're outputting. That's why "Pay" is bumped to the left by two spaces. If you don't want that, you could always do << setw(10) << "Pay" << "\n\n";.
Mar 23, 2015 at 4:39pm
but whats weird is that this, the following output:
cout << left << setw(10) << name << setw(10) << type << setw(10) << hours << setw(10) << sales << setw(10) << wages << setw(10) << pay << endl << endl << endl;

is the one that's affected by the \n\n

not this one:

cout << left << setw(10) << "Names" << setw(10) << "Status" << setw(10) << "Hours" << setw(10) << "Sales" << setw(10) << "Wages" << setw(10) << "Pay\n\n";

does it also bump this over as well because its a character even though its part of a new "cout"



when the code is put together like this:

cout << "Employee Pay Report \n\n";

cout << left << setw(10) << "Names" << setw(10) << "Status" << setw(10) << "Hours" << setw(10) << "Sales" << setw(10) << "Wages" << setw(10) << "Pay\n\n";

cout << left << setw(10) << name << setw(10) << type << setw(10) << hours << setw(10) << sales << setw(10) << wages << setw(10) << pay << endl << endl << endl;

sry i tried ran the program to try and paste the print out but it wont let me/ or i dont know how


Mar 23, 2015 at 4:57pm
Please put all of your code between code tags <> so its more readable.

http://www.cplusplus.com/articles/jEywvCM9/
Mar 23, 2015 at 4:57pm
If you're going to keep posing code, use the [code][/code] tags. Also, I'm not sure what you're asking. As for the way this works: cout << setw(10)<< "Pay\n\n";.

setw: "I'm supposed to make sure that the next thing to be output takes up 10 spaces!"
setw: "Oh, it's a string! I wonder how many characters it has?"
setw: "...I count 5 characters."
setw: "Cout! Print 5 spaces and then this string!"
cout: "OK... printing ' ', ' ', ' ', ' ', ' '..."
cout: "Printing 'P', 'a', 'y'...."
cout: "Printing... wait, this is '\n'! This doesn't get printed!"
cout: "'\n' tells me to start printing on a new line."
cout: "OK, new line! What, another '\n'? Fine, new line again!"
Mar 23, 2015 at 5:19pm
cout is an object.

<< is an operator that returns a reference to object on the left hand side.

This:
 
cout << a << b << c;
has the exact same meaning as:
 
((cout << a) << b) << c;

First (cout << a) runs, the << operator returns cout so what happens next is (cout << b) and then (cout << c);

So it doesn't matter how many times you mention cout.

This is still the same, no matter what a, b and c are:
1
2
3
cout << a;
cout << b;
cout << c;
Mar 23, 2015 at 11:31pm
Alright thanks, and ill check that link out
Topic archived. No new replies allowed.