0% found this document useful (0 votes)
31 views

Lab 1 Struct

Uploaded by

Tamara Enad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lab 1 Struct

Uploaded by

Tamara Enad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Object Oriented Programming I

Lab Assignment (Structures)

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++)
{

cout << people[i].firstName <<" " << people[i].lastName<< setw(10)<<people[i].age<<endl;


}
}

You might also like