9 Replies - 662 Views - Last Post: 08 September 2012 - 10:22 PM Rate Topic: -----

#1 Bitabith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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];
		cout<<setw(5)<<"| "<<calculatedPointGrade[i]<<setw(3)<<"|  "<<letterGrade[i]<<"      |"<<endl;
	}
}
 



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  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#3 Bitabith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 rethc  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#6 Bitabith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 988
  • View blog
  • 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;


Was This Post Helpful? 1
  • +
  • -

#8 Bitabith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  |"
		<<setw(10)<<"   Grade     |"<<endl;
	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];
				cout<<"| "<<setw(9)<<calculatedPointGrade[i]
					<<"|  "<<setw(5)<<letterGrade[i]
					<<"      |"<<endl;

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 2039
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#10 Bitabith  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1