Can you please fix this C++ code #include using namespace std; class Student { private: string name; float gp

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Can you please fix this C++ code #include using namespace std; class Student { private: string name; float gp

Post by answerhappygod »

Can you please fix this C++ code
Can You Please Fix This C Code Include Iostream Using Namespace Std Class Student Private String Name Float Gp 1
Can You Please Fix This C Code Include Iostream Using Namespace Std Class Student Private String Name Float Gp 1 (458.35 KiB) Viewed 31 times
#include <iostream>
using namespace std;
class Student {
private:
string name;
float gpa;
int enrollYear, rollNumber;
public:
Student();
void setName(string name_input);
void setGpa(float gpa_input);
void setEnrollYear(int enrollYear_input);
void setRollNumber(int rollNumber_input);
void displayStudent();
string getName();
float getGpa();
int getEnrollYear();
int getRollNumber();
};
#include "student.h"
#include "stdafx.h"
using namespace std;
void Student::setName(string name_input)
{
name = name_input;
}
void Student::setGpa(float gpa_input)
{
gpa = gpa_input;
}
void Student::setEnrollYear(int enrollYear_input)
{
enrollYear = enrollYear_input;
}
void Student::setRollNumber(int rollNumber_input)
{
rollNumber = rollNumber_input;
}
void Student::displayStudent()
{
cout<<"Name : "<<name;
cout<<"GPA : "<<gpa<<endl;
cout<<"Enrollment year :
"<<enrollYear<<endl;
cout<<"Roll Number : "<<rollNumber<<endl;
}
string Student::getName()
{
return name;
}
float Student::getGpa()
{
return gpa;
}
int Student::getEnrollYear()
{
return enrollYear;
}
int Student::getRollNumber()
{
return rollNumber;
}
#include "student.h"
#include <iostream>
#include "stdafx.h"
#define MAX_STUDENTS 5
using namespace std;
void executeAction(char);
int addStudent(string name_input, float gpa_input, int
enrollYear_input, int rollNumber_input); // 10 points
void displayStudents();
void sort();
void studentsAfterGivenYear();
Student s[MAX_STUDENTS];
int currentCount = 0;
int main()
{char choice = 'i';
do
{
cout<<"\nCSE240 HW9\n";
cout << "Please select an action:\n";
cout << "\t a: add a new student\n";
cout << "\t d: display student list\n";
cout << "\t s: sort the students by GPA\n";
cout << "\t n: display students enrolled after given
year\n";
cout << "\t q: quit\n";
cin >> choice;
cin.ignore(); // ignores the trailing \n
executeAction(choice);
} while (choice != 'q');
return 0;
}
void executeAction(char c)
{
string name_input;
float gpa_input;
int enrollYear_input, rollNumber_input, result = 0;
switch (c)
{
case 'a':
cout << "Please enter student name: ";
getline(cin, name_input);
cout << "Please enter GPA: ";
cin >> gpa_input;
cin.ignore();
cout << "Please enter enrollment year: ";
cin >> enrollYear_input;
cin.ignore();
cout << "Please enter roll number: ";
cin >> rollNumber_input;
cin.ignore();
result = addStudent(name_input, gpa_input, enrollYear_input,
rollNumber_input);
if (result == 0)
cout<<"\nThat student is already in the list or list is
full! \n\n";
else
cout << "\nStudent successfully added to the list!
\n\n";
break;
case 'd': // display the list
displayStudents();
break;
case 's': // sort the list
sort();
break;
case 'n': // find and display newest student
studentsAfterGivenYear();
break;
case 'q': // quit
break;
default: cout << c <<" is invalid input!\n";
}
}
int addStudent(string name_input, float gpa_input, int
enrollYear_input, int rollNumber_input)
{
if(currentCount < MAX_STUDENTS)
{
for(int i=0;i<currentCount;i++)
if((s.getName() == name_input) && (s.getGpa() ==
gpa_input) && (s.getEnrollYear() == enrollYear_input)
&& (s.getRollNumber() == rollNumber_input))
return 0;
Student temp;
temp.setName(name_input);
temp.setGpa(gpa_input);
temp.setEnrollYear(enrollYear_input);
temp.setRollNumber(rollNumber_input);
s[currentCount] = temp;
currentCount++;
return 1;
}
return 0;
}
void displayStudents()
{
for(int i=0;i<currentCount;i++)
{
s.displayStudent();
cout<<endl;
}
}
void sort()
{
Student temp;
int max;
for(int i=0;i<currentCount-1;i++)
{
max = i;
for(int j=i+1;j<currentCount;j++)
{
if(s[j].getGpa() > s[max].getGpa())
max = j;
}
if(max != i)
{
temp = s;
s = s[max];
s[max] = temp;
}
}
cout << endl<< "Student list sorted! Use d option to
see the sorted result."<< endl;
}
void studentsAfterGivenYear()
{
int lowerBound; // Ask the user for lower bound of enrollment
year
Student* newStudent = new Student;
cout<<"Enter the lower bound of enrollment year : ";
cin>>lowerBound;
// enter code here
for(int i=0;i<currentCount;i++)
{
if(s.getEnrollYear() >= lowerBound)
{
newStudent->setEnrollYear(s.getEnrollYear());
newStudent->setGpa(s.getGpa());
newStudent->setName(s[i].getName());
newStudent->setRollNumber(s[i].getRollNumber());
newStudent->displayStudent();
cout<<endl;
}
}
}
Q#2 . . C Write a program that templates the class Matrix. The Matrix class should have the following data and member functions: M rows & N columns Pointer to array of pointers that stores each row on the heap via one of the pointers in the array of pointers Default constructor Parametrized constructor that sets the values of Mand N and inits all elements to Value Destructor Copy constructor getRowSize() & getColSize() Overloaded assignment operator=( ) o If the row/col of the target matrix is not equal to row/col of destination matrix, print failure message and exit function Overloaded operator+() that allows two congruent matrices to be added (add the destination elements to the corresponding. target elements producing a resultant matrix of size (MN) friend overloaded function operator<<( ) that prints out matrix in elegant format After creating a working class for int, template your function. Instantiate the case of a float matrix for the following cases: Matrix A(M=8, N=8, Value=5.0) and Matrix B(M==8, N=8, Value = 10.0) Increment each element of Matrix A and Matrix B by i*Row#, where i is the row number Add matrix A+B and assign it to matrix R(M=8, N=8, Value=0) Output Matrix A, B and R . . . . . .
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply