From structure to array

Hi. i have this code and I want to put all the information input into an array and find the highest salary input. How could I do it?

Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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;
Last edited on
Question was not 100% clear. But I made this program real quick for you. Try and finish it.

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
#include <iostream> 
#include <string>

using namespace std;

struct  Person {
	string name; // Use string and not char array if you can. Don't forget to #include <string>
	int age;
	float salary;
};

int main(){

	const int size = 2; // Amount of people
	Person persons[size]; // 10 people
	
	for (int i = 0; i < size; i++) // loop through all people to get the information from them
	{
		cout << "Enter Full name: ";
		getline(cin, persons[i].name); // name of each person
		cout << "\nEnter age: ";
		cin >> persons[i].age; // age of each person
		cout << "\nEnter salary: ";
		cin >> persons[i].salary; // salary of each person
		cin.ignore();
		cout << "\n";
	}

	for (int i = 0; i < size; i++) // loop through all people to display teh information
	{
		cout << "Person " << i + 1 << " -\n";
		cout << "Name: " << persons[i].name;
		cout << "\nAge: " << persons[i].age;
		cout << "\nSalary: " << persons[i].salary << endl << endl;
	}

	//Loop through all the people and compare their salaries using if statements

	
	
	
	//Display highest salary
	
	
	return 0;
}


Here is a thread about finding the min/max value in an array - http://www.cplusplus.com/forum/beginner/16878/

You can find another hundred of these if you google it.

Last edited on
Thanks a lot! It works.

I still have a question for cin getline and cin ignore - when I use them instead of just cin ?
Maybe for the string is getline because it needs to have two names and we continue with the next cin when we press Enter?
When you use std::cin >> it stops reading input after white space. So for example if you entered the name Tarik Neaj, "Neaj" would be removed. std::getline reads input until the end.

About the cin.ignore() ---

Try removing it and see what happens. It will skip over stuff. That is because you switched from getline to cin. This is just a very vague explanation, but you can read more about it here -

http://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input
Okay thanks! I even found the min value of the salary! Thanks a lot!
Topic archived. No new replies allowed.