Facing problem in structures

Hello guys! I was studying structures. Kindly see the code below here. So it is taling information from user and displaying it! Now how will i modify it to take information from 10 people ? Thanks!

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
  #include <iostream>
using namespace std;

struct Person
{
    char name[50];
    int age;
    float salary;
};

int main()
{
    Person p1 ;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;

    return 0;
}
Make an array of type Person, that has a length of 10.
Then, use a for loop to iterate and assign values to each person.

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
Person people[10];
for (int i = 0; i < 10; i++)
{
    // access a particular person as: people[i]
    // access a particular person's name as people[i].name
    // try to incorporate your lines 15-25 in here.
}
Is that the only sensible way?
I'm not exactly sure what you're asking.

If you don't need to store the records, keeping a vector/array of them is not necessary, and you can just do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int i = 0; i < 10; i++)
{
    // your code:
    Person p1;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;
}

instead.

PS: Prefer std::strings to char arrays. Must easier to use.
Last edited on
It is an option,

You could use a container such as Vector, that would allow for it contain more than 10 or less than 10.

You could also create each Person separately such as

Person p1
Person p2
Person p3
etc...

this isn't ideal and would still require a loop to input the information or lots and lots of copy pasted code which if you looking at learning c++ it best to avoid such.
If you copy pasting a lot of code it normally a sign that there may be a better way.
I want the user to enter information of persons 1, 2,3 etc first. Then after the loop finishes, it displays information of all people. I copied your code and kept the upper lines same. It's not working. My basic aim is to understand structure in c++ and why is it useful. I have to cover class, inheritance later etc. I havent studies vectors.
Then after the loop finishes, it displays information of all people

If you need to display all the information at once, after the loop finishes, then it's only a logical conclusion that this information needs to be stored somewhere.

Storing the information in a struct is one possible solution, and I would say is the easiest. Another option (that can also last between program runs) is to store information in a file.
Can you please do that by Storing the information in a struct?
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
#include <iostream>
using namespace std;

struct Person //just 1 attribute for simplicity
{
	int age;
};

int main() 
{
	Person P[10];						//create an array, basicaly a list of P0, P1, P2....P9, note there is no P10

	for (int i = 0; i < 10; i++)		//i starts at 0, goes us to < 10 so stops at 9, i++ means it goes up 1 at a time
	{
		cout << "Enter age" << endl;	//endl is part of std, it just moves corsor to next line to display better
		cin >> P[i].age;				//we use the counter i to select the corect Person and the cin to there age.
	}

	for (int i = 0; i < 10; i++)
	{
		cout << "Person " << i << " is aged:" << P[i].age << endl;
	}

	int x; //just to pause the program
	cin >> x;

	return 0;
}


Made a simple as I can with notes to explain.
To add the further information should be straightforward enough.

The for(int i = 0; i < something; i++) is something you will find you use a lot before you start looking at stuff such as inheritance imo.
Same with arrays, you will always want to store lists of information such as this and arrays being the simplest for a beginner imo.
Okay, here's an example of storing an array of objects, based off your OP:
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
// Example program
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;

struct Person
{
    std::string name;
    int age;
    float salary;
};

int main()
{
    const int size = 3;
    Person people[size];

    for (int i = 0; i < size; i++)
    {  
        Person person;
        cout << "\nPerson " << i+1 << ":\nEnter Full name: ";
        std::getline(cin, person.name);
        cout << "Enter age: ";
        cin >> person.age;
        cout << "Enter salary: ";
        cin >> person.salary;

        // flush newline character out of buffer in preparation for next getline
        cin.ignore();
        
        people[i] = person;
    }
    
    cout << "\nDisplaying Information." << endl;
    for (int i = 0; i < size; i++)
    {
        cout << "Person " << i+1 << ":\n"
             << "  Name:   " << people[i].name << '\n'
             << "  Age:    " << people[i].age << '\n'
             << "  Salary: " << people[i].salary << endl;
    }
}


Grunalin's example is probably easier to understand.
Last edited on
Now I made this simple program with no structure asking age and salary! this will also work. But my main question is that why we are involving structure. Why it is helpful in such kind of programs cuz I just did it without using it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
int main() 
{			
   int age, salary;
  
	for (int i = 0; i < 10; i++)	
	{
		cout << "Enter age of Person " << i +1 <<  endl;	
		cin >> age;		
		cout << "Enter salary of Person " << i +1 << endl; 	
		cin >> salary;	
	}

	for (int i = 0; i < 10; i++)
	{
		cout << "Person " << i +1 << " is aged:" << age << endl;
	    cout << "Person " << i +1 << " salary is:" << salary << endl;
	}
	return 0;
}
Did you actually run your program and have it output 10 people? The answer to your question will be obvious once you do so.
(Hint: How many different ages/salaries can you print?)
Last edited on
structs let you organize your code. Loose variables (say you had 10 fields making up a person) are going to be a big pain in a bigger program where you have to pass them in and out of functions (pass 1 thing or pass 10?) or put them in containers (10 containers, or 1?). The organization of the code this way helps prevent bugs and makes the code shorter and easier to manage and understand. It also gives you a number of tools via the structs and classes that are not available without them, lets take the basic assignment operator:

would you rather have this:
person newguy = oldguy;
or this:
copyintonewperson(age1,age2, name1, name2, gender1, gender2, address1, adderess2, city1, city2, state1, state2, .... and on and on..)

you are not wrong: you can do everything that can be done by a computer in main using no containers or objects. But once that program exceeds about 50 to 100 lines, you will not be able to debug it or add more to it easily, and some programs (think, microsoft word) are tens of thousands of lines of code, on up into the millions of lines if you count the library source etc...

Last edited on
Each time through the first loop, you are storing the age and salary in the same locations.
i.e. when you enter the age and salary of the second person, you're wiping out the values entered for the first person.
Likewise, in the second loop, you printing the same values (of the last person entered) every time through the loop.

This is what arrays are for. Each time through the loop, you reference the i'th age or salary.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{   int age[10];
    float salary[10];

    for (int i = 0; i < 10; i++)
    {   cout << "Enter age of Person " << i + 1 << endl;
        cin >> age[i];
        cout << "Enter salary of Person " << i + 1 << endl;
        cin >> salary[i];
    }

    for (int i = 0; i < 10; i++)
    {   cout << "Person " << i + 1 << " is aged:" << age[i] << endl;
        cout << "Person " << i + 1 << " salary is:" << salary[i] << endl;
    }
    return 0;
}


Regarding structs, structs allow you to keep related information together. It's a way of organizing related information. Instead of having individual arrays, you can have an array of persons, each with their own name, age and salary. As jonin pointed out, as programs get larger, it's important to keep related information together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
    struct
    {   int age;
        float salary;
    } person[10]

    for (int i = 0; i < 10; i++)
    {   cout << "Enter age of Person " << i + 1 << endl;
        cin >> person[i].age;
        cout << "Enter salary of Person " << i + 1 << endl;
        cin >> person[i].salary;
    }

    for (int i = 0; i < 10; i++)
    {   cout << "Person " << i + 1 << " is aged:" << person[i].age << endl;
        cout << "Person " << i + 1 << " salary is:" << person[i].salary << endl;
    }
    return 0;
}


Regarding the original problem, let's start by moving the reading and writing code into methods:
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
#include <iostream>
using namespace std;

struct Person
{
    char name[50];
    int age;
    float salary;
    void input();
    void output(ostream &os);
};

void Person::input()
{
    cout << "Enter Full name: ";
    cin.get(name, 50);
    cout << "Enter age: ";
    cin >> age;
    cout << "Enter salary: ";
    cin >> salary;

}

void Person::output(ostream &os)
{
    os << "Name: " << name << '\n';
    os <<"Age: " << age << '\n';
    os << "Salary: " << salary << '\n';
}

int main()
{
    Person p1 ;
    p1.input();

    cout << "\nDisplaying Information." << endl;
    p1.output(cout);
    return 0;
}


Now reading and writing 10 people is trivial:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
    const int NumPeople=10;
    Person people[NumPeople];

    for (int i=0; i<NumPeople; ++i) {
        people[i].input();
    }

    cout << "\nDisplaying Information." << endl;
    for (int i=0; i<NumPeople; ++i) {
        people[i].output(cout);
    }
    return 0;
}


Last edited on
Topic archived. No new replies allowed.