# Help with setw() function

Page 1 of 1

## 9 Replies - 662 Views - Last Post: 08 September 2012 - 10:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291304&amp;s=83bedd61562cf1a60c83bfd550976bf2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bitabith

Reputation: 0
• Posts: 6
• Joined: 05-September 12

# Help with setw() function

Posted 08 September 2012 - 07:13 AM

```void showGradeData(float **scores, int num, float calculatedPointGrade[], char letterGrade[]) // displays the table with students averages and letter score
{
cout<<"|-------------------------------------------------|"<<endl;
cout<<setprecision(1)<<setiosflags(ios::fixed)<<"| SN"<<setw(9)<<"| Exam "<<setw(10)<<"|  Lab Ave "<<setw(8)<<"|  HW  "<<setw(8)<<"|  Pts. |"<<setw(10)<<"  Grd   |"<<endl;
cout<<"|-------------------------------------------------|"<<endl;
for(int i=0;i<num;i++)
{
cout<< "| "<<(i+1)<<setw(5)<<setprecision(1)<<setiosflags(ios::fixed)<<"| "<<scores[i][0]<<setw(3)<<"| "<<scores[i][1]<<setw(8)<<"| "<<scores[i][2];
}
}

```

Here is a sample of the output:

Enter number of students: 2
Enter Exam average for Student 1: 100
Enter Lab average for Student 1: 67
Enter Homework average for Student 1: 99

Enter Exam average for Student 2: 77
Enter Lab average for Student 2: 100
Enter Homework average for Student 2: 99

|-------------------------------------------------|
| SN | Exam | Lab Ave | HW | Pts. | Grd |
|-------------------------------------------------|
| 1 | 100.0 | 67.0 | 99.0 | 90.0| B |
| 2 | 77.0 | 100.0 | 99.0 | 86.1| B |
Press any key to continue . . .

Could someone please give me some advice on how to line the columns and rows up...I almost have it completed but when I enter in 100, it moves the columns so they don't line up. I think it's just a setw() problem but I am not exactly sure. Thanks

Is This A Good Question/Topic? 0

## Replies To: Help with setw() function

### #2 Skydiver

• Code herder

Reputation: 2039
• Posts: 6,072
• Joined: 05-May 12

## Re: Help with setw() function

Posted 08 September 2012 - 07:22 AM

What have you tried? What research have you done?

### #3 Bitabith

Reputation: 0
• Posts: 6
• Joined: 05-September 12

## Re: Help with setw() function

Posted 08 September 2012 - 07:33 AM

I have tried to adjust the setw() values to be equal in each column. I've read the section/tutorial on setw() and i've also asked my professor. It's been over a year since my last C++ class so i am trying to refresh myself but my time is limited because this is due soon.

### #4 rethc

Reputation: 12
• Posts: 76
• Joined: 23-April 12

## Re: Help with setw() function

Posted 08 September 2012 - 08:01 AM

Change the setw(5) in the for loop to setw(6) so it can fit 5 characters between the columns

### #5 Skydiver

• Code herder

Reputation: 2039
• Posts: 6,072
• Joined: 05-May 12

## Re: Help with setw() function

Posted 08 September 2012 - 08:01 AM

Right now, your code looks like a mess because it is hard to correlate your setw() calls to which columns you are printing out. It get's even harder because you are using one setw() value while printing out the column header and a different value for the column values.

You are on the right track of using setw(), but you'll just need to be a bit more disciplined so that you can match up the appropriate widths with for a particular column.

### #6 Bitabith

Reputation: 0
• Posts: 6
• Joined: 05-September 12

## Re: Help with setw() function

Posted 08 September 2012 - 09:44 AM

Ok thank you, i will try and format it a little more clear. But it helps knowing i am on the right track.

### #7 #define

• Duke of Err

Reputation: 988
• Posts: 3,448
• Joined: 19-February 09

## Re: Help with setw() function

Posted 08 September 2012 - 07:30 PM

Hi, use the setw before the value.

```cout << "| " << setw(5) << (i+1);

```

You can use the same setw size in the title if you use the column name.

```cout << "| " << setw(5) << " SN ";

```

Writing a column per line of code may be neater.

The flags fixed and setprecision can be implemented before other cout statements.

```cout << setprecision(1) << fixed;
cout << "|-------------------------------------------------|"<<endl;

```

### #8 Bitabith

Reputation: 0
• Posts: 6
• Joined: 05-September 12

## Re: Help with setw() function

Posted 08 September 2012 - 10:09 PM

Thank you all for your assistance, I was finally able to get the code correct. Here it is...

```cout<<"|---------------------------------------------------------------|"<<endl;
cout<<fixed<<showpoint<<setprecision(1);
cout<<setw(5)<<"|  SN  "
<<setw(8)<<"|  Exam  "
<<setw(9)<<" |   Labs    "
<<setw(8)<<"|   HW    "
<<setw(9)<<"|  Points  |"
cout<<fixed<<showpoint<<setprecision(1);
cout<<"|---------------------------------------------------------------|"<<endl;
for(int i=0;i<count;i++)
{
cout		<< "| "
<<setw(5)<<(i+1);
cout<<"| "<<setw(8)<<scores[i][0]
<<"| "<<setw(9)<<scores[i][1]
<<" | "<<setw(8)<<scores[i][2];
<<"      |"<<endl;
```

### #9 Skydiver

• Code herder

Reputation: 2039
• Posts: 6,072
• Joined: 05-May 12

## Re: Help with setw() function

Posted 08 September 2012 - 10:14 PM

And therein lies the fault...

Look at line 3, you are setting the width of 5, but outputing SN with the pipe symbol and space before SN while you output the left pipe and space first on line 13, and then set the width to 5 on line 14 to actually output the SN value. This is why I said it was hard to track the correlation between your headers and your widths. Even though you've updated it to have matching values for setw(), you still are not being consistent about what is being output after setting the width.

This post has been edited by Skydiver: 08 September 2012 - 10:17 PM

### #10 Bitabith

Reputation: 0
• Posts: 6
• Joined: 05-September 12

## Re: Help with setw() function

Posted 08 September 2012 - 10:22 PM

Hhmm...I didn't realize that until now. It outputs perfectly though, regardless if i enter 100 versus 89 for the scores, which was the problem i was having. I haven't submitted it yet so i am going to look at it some more, keeping your comments in mind.