Lab 1 Struct
Lab 1 Struct
Question#1:
#include <iostream>
#include <string>
struct PersonRec
{
string lastName;
string firstName;
int age;
};
int main( )
{
PersonRec thePerson;
cout << "Enter first name: ";
cin >> thePerson.firstName;
cout << "Enter last name: ";
cin >> thePerson.lastName;
cout << "Enter age: ";
cin >> thePerson.age;
cout << "\n\nHello " << thePerson.firstName << ' '<< thePerson.lastName << ". How are you?\n";
cout << "\nCongratulations on reaching the age of "<< thePerson.age << ".\n";
}
Question#2:
#include<iostream>
#include<string>
struct GradeRec
{
float percent;
char grade;
};
struct StudentRec
{
string lastName;
string firstName;
int age;
GradeRec courseGrade;
};
int main( )
{
StudentRec student;
cout << "Enter first name: ";
cin >> student.firstName;
cout << "Enter last name: ";
cin >> student.lastName;
cout << "Enter age: ";
cin >> student.age;
cout << "Enter overall percent: ";
cin >> student.courseGrade.percent;
if(student.courseGrade.percent >= 90)
{
student.courseGrade.grade = 'A';
}
else if(student.courseGrade.percent >= 75)
{
student.courseGrade.grade = 'B';
}
else
{
student.courseGrade.grade = 'F';
}
cout << "\n\nHello " << student.firstName << ' ' << student.lastName<< ". How are you?\n";
cout << "\nCongratulations on reaching the age of " << student.age<< ".\n";
cout << "Your overall percent score is "<< student.courseGrade.percent << " for a grade of "
<< student.courseGrade.grade;
}
Question#3:
#include <iostream>
#include <string>
#include <iomanip>
struct PersonRec
{
string lastName;
string firstName;
int age;
};
typedef PersonRec PeopleArrayType[10]; //an array of 10 structs
int main()
{
PeopleArrayType people; //a variable of the array type
for (int i = 0; i < 3; i++)
{
cout << "Enter first name: ";
cin >> people[i].firstName;
cout << "Enter last name: ";
cin >> people[i].lastName;
cout << "Enter age: ";
cin >> people[i].age;
}
for (int i = 0; i < 3; i++)
{