In: Computer Science
USING C++
Create a program that defines a struct for a student. The following data should be part of the struct: studentID (int), gpa(double), fullName(string), units(int).
Instantiate two students and have the user type in information for each, then print both back to the screen afterwards. Input validation: studentID should be positive and have 4 digits, GPA should be between 0 and 4 (inclusive), units should be positive. Have any invalid inputs reentered.
#include <iostream>
using namespace std;
struct Student{
int studentID;
double gpa;
string fullName;
int units;
};
struct Student getStudent(){
Student s;
int id,units;
double gpa;
string name;
while(true){
cout<<"Enter ID: ";
cin>>id;
if(id>=1000 &&
id<=9999)
break;
cout<<" Please Enter the ID
between 1000 and 9999\n";
}
while(true){
cout<<"Enter Gpa: ";
cin>>gpa;
if(gpa>=0 &&
gpa<=4)
break;
cout<<" Please Enter the GPA
between 0 and 4\n";
}
cout<<"Enter name:";
cin>>name;
while(true){
cout<<"Enter Gpa: ";
cin>>units;
if(units>=0)
break;
cout<<" Please Enter the
Units greater than 0\n";
}
s.studentID=id;
s.gpa=gpa;
s.fullName=name;
s.units=units;
return s;
}
void display(Student &s){
cout<<"ID:
"<<s.studentID<<endl;
cout<<"Name:
"<<s.fullName<<endl;
cout<<"GPA: "<<s.gpa<<endl;
cout<<"Units:
"<<s.units<<endl;
}
int main(){
Student s1=getStudent();
Student s2=getStudent();
display(s1);
display(s2);
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me