Setw not lining up correctly

I'm using an input file which reads

Jane Smith 100
Rose Diaz 200
John Williams 150
Felicia Johnson 250
Ron Davis 75

I am trying to output that file into another file using arrays but it never lines up correctly. This is how it always ends up looking like.
Any advice?


Jane          Smith              100.00
Rose           Diaz              200.00
John       Williams              150.00
Felicia        Johnson              250.00
Ron          Davis               75.00




1
2
3
4
5
6
7
8
9
10
  void SalesReport(string outputfilename, string firstname[], string lastname[], double Sales[], int ArraySize)//missing parameters
{
	ofstream OutputFile; //sets output data file varible
	OutputFile.open(outputfilename); //sets name for output variable
	for (int i = 0; i <= 4; i++)
	{
		OutputFile << fixed << setprecision(2);
		OutputFile << firstname[i] << setw(15) << lastname[i] << setw(20) << Sales[i] << "\n";//trying to set up data from inputfile to read out onto file
	}
}
Last edited on
You should probably use setw() for each field, including the first.

Perhaps something more like:

1
2
3
4
5
6
7
8
9
10
    std::stringstream sin("Jane Smith 100\nRose Diaz 200\nJohn Williams 150\nFelicia Johnson 250\nRon Davis 75");
    cout << setprecision(2) << fixed;
    std::string first, second;
    double amount;
	while(sin >> first >> second >> amount)
	{
		cout << fixed << setprecision(2);
		cout << left << setw(12) << first << setw(12) << second << right 
             << setw(6) << amount << "\n";
	}


which produces output like:

1
2
3
4
5
Jane        Smith       100.00
Rose        Diaz        200.00
John        Williams    150.00
Felicia     Johnson     250.00
Ron         Davis        75.00



Thank you for your advice. I tried putting left into my code and setw before the first name like you showed and it worked. I am kinda struggling to understand how it fixed it but ill just have to do more research, Thankyou.

SOLUTION
1
2
3
4
5
6
7
8
9
10
void SalesReport(string outputfilename, string firstname[], string lastname[], double Sales[], int ArraySize)//missing parameters
{
	ofstream OutputFile; //sets output data file varible
	OutputFile.open(outputfilename); //sets name for output variable
	for (int i = 0; i <= 4; i++)
	{
		OutputFile << fixed << setprecision(2);
		OutputFile << left << setw(15) << firstname[i] << setw(15) << lastname[i] << setw(20) << Sales[i] << "\n";//trying to set up data from inputfile to read out onto file
	}
}



Jane           Smith          100.00              
Rose           Diaz           200.00              
John           Williams       150.00              
Felicia        Johnson        250.00              
Ron            Davis          75.00               
Last edited on
Actually i switched it to Right instead of left and setw(7) and it looks even better

1
2
3
4
5
6
7
8
9
10
void SalesReport(string outputfilename, string firstname[], string lastname[], double Sales[], int ArraySize)//missing parameters
{
	ofstream OutputFile; //sets output data file varible
	OutputFile.open(outputfilename); //sets name for output variable
	for (int i = 0; i <= 4; i++)
	{
		OutputFile << fixed << setprecision(2);
		OutputFile << right << setw(7) << firstname[i] << setw(15) << lastname[i] << setw(20) << Sales[i] << "\n";//trying to set up data from inputfile to read out onto file
	}
}



   Jane          Smith              100.00
   Rose           Diaz              200.00
   John       Williams              150.00
Felicia        Johnson              250.00
    Ron          Davis               75.00

Topic archived. No new replies allowed.