1.Write a program that uses two structures Name and Student to store the followi
ID: 3779711 • Letter: 1
Question
1.Write a program that uses two structures Name and Student to store the following information for multiple students:
Create a NAME structure that consists of
First Name,
Middle Initial, and
Last Name.
Create a STUDENT structure that contains student information (Include the NAME structure within the Student information structure):
Name
ID
SSN
Program (CSCI, DBMS, INFM, SDEV)
GPA (between 0.0 and 4.0)
Class Year (Freshman, Sophomore, Junior, Senior)
In the main program create at least two students. The program should prompt the user for the input for one student and hard code the values for the second.
VALIDATE USER INPUT!
Display all student information in a tabular form when all students have been entered.
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
struct Name
{
string firstName;
string middleInitial;
string lastName;
};
struct Student
{
struct Name name; //nested structure
int ID;
string email;
string SSN;
string Program;
float GPA;
string classYear;
};
int main()
{
struct Student student1,student2;
string fname,mname,lname,email,ssn,prg,classyear;
float gpa;
int id;
cout<<"Enter Details of student1"; // input details of student 1
cout<<" Enter Name ";
cin>>fname>>mname>>lname;
student1.name.firstName = fname;
student1.name.middleInitial = mname;
student1.name.lastName = lname;
cout<<" Enter ID";
cin>>id;
student1.ID = id;
cout<<" Enter email";
cin>>email;
student1.email = email;
cout<<" Enter SSN";
cin>>ssn;
student1.SSN = ssn;
cout<<" Enter program enrolled";
cin>>prg;
if(prg != "CSCI" && prg!= "DBMS" && prg!= "INFM" && prg != "SDEV") //program validation
{
cout<<" Invalid input";
exit(0);
}
student1.Program = prg;
cout<<" Enter GPA ";
cin>>gpa;
if(gpa <0.0 || gpa>4.0) //GPA validation
{
cout<<" Invalid input";
exit(0);
}
student1.GPA = gpa;
cout<<" Enter Class";
cin>>classyear;
if(classyear !="Freshman" && classyear!="Sophomore" && classyear!="Junior" && classyear!= "Senior")
{
cout<<" Invalid input"; //classYear validation
exit(0);
}
student1.classYear = classyear;
cout<<" student name : "<<student1.name.firstName<<" "<<student1.name.middleInitial<<" "<<student1.name.lastName;
cout<<" Id : "<<student1.ID;
cout<<" Email :"<<student1.email;
cout<<" SSN :"<<student1.SSN; //display student1 data
cout<<" Program "<<student1.Program;
cout<<" GPA :"<<student1.GPA;
cout<<" Class Year :"<<student1.classYear;
student2.name.firstName ="John";
student2.name.middleInitial = "M.";
student2.name.lastName = "Black";
student2.ID =1005;
student2.email ="john@hotmail.com";
student2.SSN = "ss1224-555";
student2.Program ="DBMS";
student2.GPA =3.45;
student2.classYear ="Junior";
//display student 2 data
cout<<" student name : "<<student2.name.firstName<<" "<<student2.name.middleInitial<<" "<<student2.name.lastName;
cout<<" Id : "<<student2.ID;
cout<<" Email :"<<student2.email;
cout<<" SSN :"<<student2.SSN;
cout<<" Program "<<student2.Program;
cout<<" GPA :"<<student2.GPA;
cout<<" Class Year :"<<student2.classYear;
return 0;
}
output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.