Please correct my code, that have some wrong. Show in full code and explain wron
ID: 3539636 • Letter: P
Question
Please correct my code, that have some wrong.
Show in full code and explain wrong in line using comment syntax.
Thank.
My code:
#include <iostream>
using namespace std;
class Marks
{
private:
char * name, grade[];
float gpa;
public:
char * set_name(char *n)
{
name=n;
cout<<"Enter name : ";
cin>>name;
return name;
}
float set_gpa(float g)
{
gpa=g;
cout<<"Enter CGPA :";
cin>>gpa;
return gpa;
}
char set_grade(char grad[])
{
if(gpa>=3.00&&gpa<=4.00)
{
grade[0]='A';
}
else if(gpa>=2.00&&gpa<=3.00)
{
grade[0]='B';
}
else if(gpa>=0.00&&gpa<=1.99)
{
grade[0]='F';
}
}
};
int main()
{
Marks M;
cout<<"==================================="<<endl;
cout<<"ENTER INFORMATION "<<endl;
cout<<"==================================="<<endl;
M.set_name(n);
M.set_gpa(g);
M.set_grade();
cout<<"==================================="<<endl;
cout<<"RESULT SLIP "<<endl;
cout<<"==================================="<<endl;
cout<<"Name :"<<M.name<<endl;
cout<<"GPA :"<<M.gpa<<endl;
cout<<"Grade :"<<M.grade<<endl;
return 0;
}
Explanation / Answer
//Here is the updated code please go through it. Ask in case of any more doubts.
#include<iostream>
#include<cstdio>
using namespace std;
class Marks
{
private:
char* name;//Name should be a string or char*
char grade;//grade should be a single character
float gpa;
public:
//char * set_name(char *n)
/**No need to take name as a parameter as we are taking
* the name from the user as input in this function itself**/
char * set_name()
{
//name=n; Not needed
cout<<"Enter name : ";
cin.getline(name,100);
return name;
}
//float set_gpa(float g)
/**No need to take name as a parameter as we are taking
* the name from the user as input in this function itself**/
float set_gpa()
{
//gpa=g; Not needed
cout<<"Enter CGPA :";
cin>>gpa;
return gpa;
}
//char set_grade(char grad[])
/**We don't need to get grade as a parameter as we just need
* the GPA for calculating the grade which is availabkle from
* the class itself**/
char set_grade()
{
if(gpa>=3.00&&gpa<=4.00)
{
grade='A';
}
else if(gpa>=2.00&&gpa<=3.00)
{
grade='B';
}
else if(gpa>=0.00&&gpa<=1.99)
{
grade='F';
}
//We need to return the grade
return grade;
}
};
int main()
{
Marks M;
//Variables to store the information about the student
char* name;
float gpa;
char grade;
cout<<"==================================="<<endl;
cout<<"ENTER INFORMATION "<<endl;
cout<<"==================================="<<endl;
/**name = M.set_name(n);
* gpa = M.set_gpa(g);
* No need to pass any parameter to the functions
* Inputs are taken in the functions themselves*/
name = M.set_name();
gpa = M.set_gpa();
grade = M.set_grade();
cout<<"==================================="<<endl;
cout<<"RESULT SLIP "<<endl;
cout<<"==================================="<<endl;
cout<<"Name :"<<name<<endl;
cout<<"GPA :"<<gpa<<endl;
cout<<"Grade :"<<grade<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.