Need help with setw ()

Jan 16, 2014 at 12:15pm
Hi there, I have got an assignment, our teacher told us to create a program which displays result based on the inputs and displays the data, he told us to use setw to align the output, I am getting a problem.
For example,This is the output I get:
Name:      Total marks:  Marks obtained:  Grade:
NameExample                30             20            B
NameEx              30             15             D 
NameExxx             30             10            F

I want to get it in the proper order, no matter how many alphabets does the name have,like this:
Name:        Total marks:  Marks obtained:  Grade:
NameExample        30             20            B
NameEx             30             15            D 
NameExxx           30             10            F

I have tried changing the value of setw, but it doesn't help, rest of the program works fine, I just need help with aligning.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include <iomanip>
#include <iostream>
using namespace std;
int main(){
	int marks[3],tmarks=30,snum=0,avg=0,highmarks=0,marksum=0,fail=0;
	char grade[3];
	string name[3];
	for(int i=0;i<3;i++){
		cout<<"Enter name of the student: "<<endl;
		cin>>setw(20)>>name[i];
		cout<<"Enter marks of "<<name[i]<<""<<endl;
		cin>>setw(20)>>marks[i];
		if(marks[i]>highmarks){
			highmarks=marks[i];
		}
				if(marks[i]>=25){
			grade[i]='A';
		}
		else if(marks[i]>=20&&marks[i]<25){
			grade[i]='B';
		}
		else if(marks[i]>=17&&marks[i]<20){
			grade[i]='C';
		}
		else if(marks[i]>=15&&marks[i]<17){
			grade[i]='D';
		}
		else if(marks[i]>=12 && marks[i]<15){
			grade[i]='F';
		}
		else if (marks[i]<12){
			grade[i]='F';
			fail++;
		}
		marksum+=marks[i];
		snum++;
	}
	avg=marksum/3;
			cout<<"Name:"
			<<setw(30)<<"Total Marks:"
			<<setw(20)<<"Marks obtained:"
			<<setw(17)<<"Grade:"<<endl;
		    for(int i=0;i<3;i++){
		    	cout<<name[i]
	                <<setw(25)<<tmarks
				    <<setw(15)<<marks[i]
				    <<setw(20)<<grade[i]<<endl;
		    }
	cout<<"                               "<<
		  "                                "<<endl;
	cout<<"Summary:             "<<endl;
	cout<<"Number of students: "<<snum<<endl;
	cout<<"Highest marks: "<<highmarks<<endl;
	cout<<"Sum of marks: "<<marksum<<endl;
	cout<<"Average is: "<<avg<<endl;
	cout<<"Failed students: "<<fail<<endl;
	return 0;
}
Jan 16, 2014 at 12:23pm
You forgot to set the width (with std::setw) of the name column.
Jan 16, 2014 at 4:10pm
I don't get it, i have used
using namespace std;
Do i still need to use std::setw?
Last edited on Jan 16, 2014 at 4:13pm
Jan 16, 2014 at 4:26pm
No, you don't need to write std:: in front. What I meant was that you didn't use setw before outputting the names.
1
2
3
4
cout << setw(10) << name[i]
     << setw(25) << tmarks
     << setw(15) << marks[i]
     << setw(20) << grade[i] << endl;


You might also want use std::left and std::right to align the names to the left and the numbers to the right.
Last edited on Jan 16, 2014 at 4:28pm
Jan 16, 2014 at 5:51pm
Peter87's solution is probably best, but I've also had luck with the use of \t.
Jan 16, 2014 at 5:53pm
Don't use \t for internal spacing - different consoles, text editors, IDEs, etc. all treat tabs differently and some even allow the user to change the treatment of tabs. Are tabs 2, 3, 4, 8 characters? Do they always add that many characters or just enough to get to the next alignment point? You never know.
Jan 16, 2014 at 6:23pm
Thanks a bunch, it worked.
Jan 16, 2014 at 6:35pm
Good point, LB.
Topic archived. No new replies allowed.