3. Simple C++ Coding I have this code: #include <iostream> #include <string> usi
ID: 3581880 • Letter: 3
Question
3. Simple C++ Coding
I have this code:
#include <iostream>
#include <string>
using namespace std;
// Declaring the structure
struct MarksCard{
string name; // Name of student
int roll_no; // Student ID number
int* tests; // Pointer to an array of test scores
double average; // Average of test scores
char grade; // Grade for the course
};
//DataTypes associated
int StudentsInfo(); // declaring the function for number of students
int TestsInfo(); // declaring the function for number of test scores
string NameInfo(); // declaring the function for each student's name
int IDInfo(); // declaring the function for each student's ID number
int *ScoresInfo(int); // declaring the function for the test scores for each student
double AverageCal(int*, int); // Calculates the average test score for each student
char calcGrade(double); // Calculates the letter grade for each student based on their average
void display(MarksCard*, int); // Displays each student's name, ID#, average test score, and course grade
int main(){
MarksCard *ListofStudent;
int size = StudentsInfo();
ListofStudent = new MarksCard[size];
if (ListofStudent == NULL){
cout << "Memory Allocation Error!"; // Check for possible memory allocation errors. If error is found, end program.
// system("pause");
return 0;
}
// Create a variable array to hold the number of test scores for each student.
int numOfTests = TestsInfo();
for (int count = 0; count < size; count++){
ListofStudent[count].name = NameInfo(); //We are calling all the functions associated with struct ListofStudent in loop.
ListofStudent[count].roll_no = IDInfo();
ListofStudent[count].tests = ScoresInfo(numOfTests);
ListofStudent[count].average = AverageCal(ListofStudent[count].tests, numOfTests);
ListofStudent[count].grade = calcGrade(ListofStudent[count].average);
}
display(ListofStudent, size);
delete [] ListofStudent;
// system ("pause");
return 0;
}
int StudentsInfo(){
int students;
cout << "How many students are there in the class?" << endl;
cin >> students;
return students;
}
int TestsInfo(){
int tests;
cout << "How many tests will be conducted during the semester?" << endl;
cin >> tests;
return tests;
}
string NameInfo(){
string name;
cout << "Enter the student's name in the class: ";
cin >> name;
return name;
}
int IDInfo(){
int roll_no;
cout << "Enter the student's ID Number: ";
cin >> roll_no;
return roll_no;
}
int *ScoresInfo(int numOfTests){
int *scores;
scores = new int[numOfTests];
cout << "Enter the test scores for the student. Press ENTER after each score." << endl;
for (int count = 0; count < numOfTests; count++){
cout << "Score " << (count + 1) << ": ";
cin >> scores[count];
}
return scores;
}
double AverageCal(int *testScores, int numOfTests){
int total = 0;
double average;
for (int count = 0; count < numOfTests; count++){
total += testScores[count];
}
average = total/numOfTests;
return average;
}
char calcGrade(double average){
char letterGrade;
if (average > 90&& average <= 100)
letterGrade = 'A';
else if (average > 80 && average <= 90)
letterGrade = 'B';
else if (average > 70 && average <= 80)
letterGrade = 'C';
else if (average > 60 && average <= 70)
letterGrade = 'D';
else if (average >= 0 && average <= 60)
letterGrade = 'F';
else{
cout << "Logic error."<< endl;
// system("pause");
exit(EXIT_FAILURE);
}
return letterGrade;
}
void display(MarksCard *ListofStudent, int size){
for (int count = 0; count < size; count++)
cout << ListofStudent[count].name << " " << ListofStudent[count].roll_no << " "
<< ListofStudent[count].average << " " << ListofStudent[count].grade << endl;
return;
}
However I get these errors:
3.cpp: In function ‘char calcGrade(double)’:
3.cpp:142:14: error: ‘EXIT_FAILURE’ was not declared in this scope
exit(EXIT_FAILURE);
^
3.cpp:142:26: error: ‘exit’ was not declared in this scope
exit(EXIT_FAILURE);
^
Explanation / Answer
Hello,
I didn't made any changes to your code However it ran smoothly without any errors or exceptions.
Here goes the output I got for your code:
Output:
How many students are there in the class?
5
How many tests will be conducted during the semester?
2
Enter the student's name in the class: santosh
Enter the student's ID Number: 45
Enter the test scores for the student. Press ENTER after each score.
Score 1: 56
Score 2: 56
Enter the student's name in the class: hello
Enter the student's ID Number: 12
Enter the test scores for the student. Press ENTER after each score.
Score 1: 12
Score 2: 12
Enter the student's name in the class: sdas
Enter the student's ID Number: 45
Enter the test scores for the student. Press ENTER after each score.
Score 1: 45
Score 2: 45
Enter the student's name in the class: sdasd
Enter the student's ID Number: 78
Enter the test scores for the student. Press ENTER after each score.
Score 1: 78
Score 2: 78
Enter the student's name in the class: gh
Enter the student's ID Number: 65
Enter the test scores for the student. Press ENTER after each score.
Score 1: 65
Score 2: 65
santosh 45 56 F
hello 12 12 F
sdas 45 45 F
sdasd 78 78 C
gh 65 65 D
I didn't get any compilation errors either. I executed it online in http://cpp.sh/ and I didn't faced any issues.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.