Question

I would like to have a clean formatted output, but the result is not exactly as clean as I want. Below is my function which shows the output, followed by the output. I am using Dev-C++. Thank you for your help.

void student::show(string name, int age, double GPA)
{
  cout<<name<<setw(7) <<age<<setw(7) <<GPA <<setw(7) <<endl;

}



******************output*******************************************



Timaya     24   2.65
 albert     29   2.43
zamounda     27   3.94
charles     26   2.78
 moumou     33   2.39
francois     24   3.07
   john     20   2.59
   papa     21   2.67
   peti     29   3.65
francoise     26   3.76
  iness     28   2.65
  elise     35   2.76
------------------------------------
-------------------------------------
GPA of the class:2.94
Was it helpful?

Solution

Since setw sets the width for the next output value, not for the previous one, you should move setw calls to before the items being formatted:

cout << setw(20) << name << setw(7) << age << setw(7) << GPA << endl;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top