Making output line up.

I know this may be a dumb question, but I've tried many ways to make this line up properly. I'm using a structure variable that contains different members based on student records. The output for this function looks horrific as of right now, and I'm not sure what I'm missing. Any suggestions are greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 void balance (StudentInformationRecord s[], int n)
{
   double threshold;
   int i;
   cout << "Enter threshold value for balance: $";
   cin  >> threshold;
   for (i=0; i <= n-1; i++)
   {
      if (s[i].balance >= threshold)
	  {
	     cout << fixed << setprecision(2);
	     cout << setw(3) << right << i+1 << "." << setw(20) << left << s[i].lastName
		      << " " << setw(20) << left << s[i].firstName << " " << setw(8) << right << s[i].hometown
			  << " " << setw(8) << right << s[i].balance;
		 
	  }
	  
   }
}
What does your output look like? What do you want it to look like? What does the StudentInformationRecord struct look like?
1
2
3
4
5
6
7
8
9
10
11
struct StudentInformationRecord
{
   string     lastName;  // field definitions of the structure
   string     firstName;
   string     hometown;
   string     major;
   double     balance;
   double     gpa;
   int        earnedHours;
   int        studentNumber;
};


The output is all pushed together and mixed up right now. I want it to look something like
1. Smith John Jamestown 450.12
or something similar with enough spacing in between for large names.
The actual data file for StudentInformationRecord looks like this:
"Smith John Jamestown BIOL 33871 556.75 38" with more spacing in between each.
Those are last name, first name, hometown, major, student number, account balance, and earned hours.
You need a newline in there somewhere. Try ending your long cout with << endl;, right after you print s[i].balance.
EDIT: I took the if statement from the code you were working on and laid it out differently in my text editor to make it easier to read. I also removed the " " characters and made the width of hometown 10 so that Jamestown would fit. See if this makes it easier for you:
1
2
3
4
5
6
7
8
9
10
        if (s[i].balance >= threshold)
        {
            cout << fixed << setprecision(2);
            cout << setw(3) << right << i+1 << "."
                 << setw(20) << left << s[i].lastName
                 << setw(20) << left << s[i].firstName
                 << setw(10) << right << s[i].hometown
                 << setw(8) << right << s[i].balance
                 << endl;
        }
Last edited on
Thanks a lot. That fixed it.
Topic archived. No new replies allowed.