How to make straight?

Ages are not straight, how to fix?

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <iomanip> 

using namespace std;

class SIX
{
private: struct PERSON
{
			 string name;
			 int age;
			 float gpa;
};
		 PERSON p[4];
public: //read data into array p 
	void ReadData()
	{
		for (int i = 0; i < 4; ++i)
		{
			cout << "Enter a name and age: ";
			cin >> p[i].name >> p[i].age;
		}
	}
	//display array p
	void Display()
	{
		cout << "  " << "NAME" << setw(10) << "AGE" << endl;
		cout << "  " << "-------------------" << endl; 
		for (int i = 0; i < 4; ++i)
		{
			cout << "  " << p[i].name << setw(10) << p[i].age; cout << endl; 
		}
	}
	//find their age average
	int AgeAve()
	{
		int total = 0;
		for (int i = 0; i < 4; ++i)
			total += p[i].age;
		return total / 4;
	}
	//display the name of those whose age is above average
	void DisplayAboveAve()
	{
		int total = 0; 
		int AgeAverage;
		for (int i = 0; i < 4; ++i)
		{
			total += p[i].age;
			AgeAverage = total / 4; 
			if (p[i].age > 24)
				cout << p[i].name << " ";
		}
		cout << endl;
	}
};
int main()
{
	int AgeAverage; 
	SIX t;
	t.ReadData();
	cout << " This is the list of people" << endl;
	t.Display();
	t.AgeAve();
	cout << "The following people age is above average ";
	cout << endl;
	t.DisplayAboveAve(); 
	
	system("pause");
	return 0; 

}
Last edited on
Please explain "straight".
I'm sorry, I don't understand your question.
Please try to explain what you do and what your problem is
So the output basically comes out like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter a name and age: John 22
Enter a name and age: Mary 19
Enter a name and age: Cynthia 33
Enter a name and age: George 25

This is a list of people
NAME       AGE

John        22

Mary        19

Cynthia        33

George        25

The following people age is above the average :
Cynthia    George
Press any key to continue . . .


How do I make the ages stay straight like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter a name and age: John 22
Enter a name and age: Mary 19
Enter a name and age: Cynthia 33
Enter a name and age: George 25

This is a list of people
NAME       AGE

John        22

Mary        19

Cynthia     33

George      25

The following people age is above the average :
Cynthia    George
Press any key to continue . . .
Last edited on
The problem is that the first column (name) does not have a fixed width.

Option A: set width for the name and probably left-align too.
Option B: adjust width of age-column with length of the name. setw(N-name.size())


Your program is awfully hard-coded for 4 persons. At least the later functions should get the person-count as parameter.

How likely is the average to be an integer? Use float. The division has to be float too. See static_cast<float>
Wait how do I do the first option A: you said ?


1
2
3
cout << "  " << "NAME" << setw(10) << "AGE" << endl;
		cout << "  " << "-------------------" << endl; 
What do you mean left-align?
hope this helps:
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <iomanip> 
#include <string.h>

using namespace std;

class SIX
{
private: struct PERSON
{
			 char name[20];//you can extend it later
			 int age;
			 float gpa;
};
		 PERSON p[4];
public: //read data into array p 
	void ReadData()
	{
		for (int i = 0; i < 4; ++i)
		{
			cout << "Enter a name and age: ";
			cin >> p[i].name >> p[i].age;
		}
	}
	//display array p
	void Display()
	{   
		cout << "  " << "NAME" << setw(19) << "AGE" << endl;
		cout << "  " << "-------------------------" << endl; 
		for (int i = 0; i < 4; ++i)
		{
			cout << "  " << p[i].name;   //now going for strlen;
			int siz;
			siz=strlen(p[i].name);
			for(int p=0;p<(20-siz);p++)  //just hope that the size of name is less than 20 or you can increase it accordingly
			cout<<" ";
			cout<< p[i].age; 
			cout << endl;
			    
			 
			
		}
	}
	//find their age average
	int AgeAve()
	{
		int total = 0;
		for (int i = 0; i < 4; ++i)
			total += p[i].age;
		return total / 4;
	}
	//display the name of those whose age is above average
	void DisplayAboveAve()
	{
		int total = 0; 
		int AgeAverage;
		for (int i = 0; i < 4; ++i)
		{
			total += p[i].age;
			AgeAverage = total / 4; 
			if (p[i].age > 24)
				cout << p[i].name << " ";
		}
		cout << endl;
	}
};
int main()
{
	int AgeAverage; 
	SIX t;
	t.ReadData();
	cout << " This is the list of people" << endl;
	t.Display();
	t.AgeAve();
	cout << "The following people age is above average ";
	cout << endl;
	t.DisplayAboveAve(); 
	
	system("pause");
	return 0; 

}


PS: I got this idea from the relative velocity principle (in physics)
@programmer007:
There is no reason whatsoever to replace the std::string with a char array. The string offers everything the array has and more. The array is not safe.


Some methods (the reference documentation explains all those manipulators):
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
#include <string>
#include <iostream>     // std::cout, std::left, std::right
#include <iomanip>      // std::setfill, std::setw

constexpr int N = 20;
constexpr int M = 6;

void filler() {
    std::cout << std::setfill ('-') << std::setw( N ) << '-' << '\n' << std::setfill (' ');
}

int main () {
    using std::cout;
    using std::setw;
    using std::left;
    using std::right;

    const std::string word = "no";
    const std::string phrase = "Hello world";

    filler();
    cout << word   << setw( N - word.size() )   << 123 << '\n';;
    cout << phrase << setw( N - phrase.size() ) << 54321 << '\n';;
    filler();
    cout << setw( N-M ) << left << word   << setw( M ) << right << 7 << '\n';;
    cout << setw( N-M ) << left << phrase << setw( M ) << right << 42 << '\n';;
    filler();
    cout << word+phrase;
    for ( int x = (word+phrase).size(); x < N-M; ++x ) cout << ' ';
    cout << setw( M ) << 101 << '\n';
    filler();

    return 0;
}
Oh I didn't knew that... Thanxx @keskiverto.
PS: I a a newbie and I dont know much about the <string> stuff...
Last edited on
Topic archived. No new replies allowed.