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!
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.
}
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.
#include <iostream>
usingnamespace 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.
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.
#include <iostream>
usingnamespace 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?)
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...
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.
#include <iostream>
usingnamespace 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.