#include <iostream>
#include <string>
usingnamespace 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(){
constint 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;
}
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 -