Problem 1: Write a program that asks the user to input student\'s IDs, and their
ID: 3648283 • Letter: P
Question
Problem 1:Write a program that asks the user to input student's IDs, and their grade for two home-works. The
program will output the ID entered, the grade for homework 1, the grade for homework 2, and
the total grade (adding up the two grades). The file name should be HW1A.cpp. You can assume there can only be a max of 2 students in the class.
Problem 2:
Same program as above but output everybody's grade at the end. You can assume there can only be
a max of 2 students in the class. Name this file HW1B.cpp
Explanation / Answer
//HW1A.cpp
#include <iostream>
using namespace std;
//get student info from the user
void getStudentInfo(int& id, float& hw1, float& hw2, float& grade){
cout << "Enter the student's ID: ";
cin>>id;
cout<<"Enter the grade for homework 1: ";
cin>>hw1;
cout<<"Enter the grade for homework 2: ";
cin>>hw2;
grade=hw1+hw2;
return;
}
int main ()
{
int id=0;
float hw1=0;
float hw2=0;
float grade=0;
//get one student at a time fromt he user, then print out the information
getStudentInfo(id,hw1,hw2,grade);
std::cout << "Student ID: " << id << " HW1: " <<hw1<<" HW2: "<<hw2<<" Grade: "<<grade<<endl;
getStudentInfo(id,hw1,hw2,grade);
std::cout << "Student ID: " << id << " HW1: " <<hw1<<" HW2: "<<hw2<<" Grade: "<<grade<<endl;
}
//HW1B.cpp
#include <iostream>
using namespace std;
//This function gets the input from the user.
void getStudentInfo(string& id, float& hw1, float& hw2){
cout << "Enter the student's ID: ";
cin>>id;
cout<<"Enter the grade for homework 1: ";
cin>>hw1;
cout<<"Enter the grade for homework 2: ";
cin>>hw2;
return;
}
//create a student class
class Student{
string id;
float hw1,hw2;
public: //the student class needs these functions
void set_values(string,float,float);
float grade() {return (hw1+hw2);}
void print_values();
};
void Student::set_values(string student_id,float h1, float h2){
id=student_id;
hw1=h1;
hw2=h2;
}
void Student::print_values(){
cout<<"Student ID: "<<id<<" HW1: "<<hw1<<" HW2: "<<hw2<<" Grade: "<<grade()<<endl;
}
int main ()
{
//create two students since that's all we need for the homework
Student student1;
Student student2;
string id="";
float hw1=0;
float hw2=0;
float grade=0;
getStudentInfo(id,hw1,hw2);//get student1 info from user
student1.set_values(id,hw1,hw2);//update student1 object
getStudentInfo(id,hw1,hw2);
student2.set_values(id,hw1,hw2);
//print both students
student1.print_values();
student2.print_values();
}