How would I implement this maximumGrade function? - c++

#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
#include "CourseGrade.h"
using namespace std;
CourseGrade* maximumGrade(CourseGrade* course0, CourseGrade* course1)
{
}
int main()
{
cout << "Course Grade App!" << endl;
cout << "--------------------------" << endl;
cout << endl;
//Prompting and creating CourseGrade objects and pointer values from inputs
int c1;
float g1;
cout << "Please enter the first course and its grade: ";
cin >> c1 >> g1;
CourseGrade Course0(c1, g1);
CourseGrade* ptrCourse0;
ptrCourse0 = &Course0;
int c2;
float g2;
cout << "Please enter the second course and its grade: ";
cin >> c2 >> g2;
CourseGrade Course1(c2, g2);
CourseGrade* ptrCourse1;
ptrCourse1 = &Course1;
int c3;
float g3;
cout << "Please enter the third course and its grade: ";
cin >> c3 >> g3;
CourseGrade Course2(c3, g3);
CourseGrade* ptrCourse2;
ptrCourse2 = &Course2;
cout << "-----------------------------------" << endl;
cout << "Course" << setw(10) << "Grade" << endl;
cout << "-----------------------------------" << endl;
cout << ptrCourse0->getCourse() << setw(10) << ptrCourse0->getGrade() << endl;
cout << ptrCourse1->getCourse() << setw(10) << ptrCourse1->getGrade() << endl;
cout << ptrCourse2->getCourse() << setw(10) << ptrCourse2->getGrade() << endl;
cout << "-----------------------------------" << endl;
cout << "The course with the maximum grade is: " << maximumGrade(ptrCourse0, ptrCourse1) << endl;
cout << "The average grade is: " << (ptrCourse0->getGrade() + ptrCourse1->getGrade() + ptrCourse2->getGrade()) / 3 << endl;
}
// End of main.cpp
void CourseGrade::setCourse(int c)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
}
void CourseGrade::setGrade(float g)
{
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
}
int CourseGrade::getCourse() const
{
return course;
}
float CourseGrade::getGrade() const
{
return grade;
}
CourseGrade::CourseGrade(int c, float g)
{
if (c >= 1000 && c <= 9999)
{
course = c;
}
else
{
course = 1000;
}
if (g >= 0.00 && g <= 100.00)
{
grade = g;
}
else
{
grade = 0.00;
}
}
Can you guys please help me out? I have three objects due to the prompt, but it only asks for two pointers? I am completely unaware of how to get the maximumGrade to show the course with the largest grade. I have tried using if statements to compare the grades of the two pointers showing the values of the grades. It HAS to use pointers to compare the course grades. Thank you guys!

I have three objects due to the prompt, but it only asks for two pointers?
The only way to make sense of two pointers passed to maximumGrade is to assume that these are the beginning and end of an array which contains the three objects.
CourseGrade courses[3] = { Course0, Course1, Course2 };
cout << "The course with the maximum grade is: "
<< maximumGrade(courses, courses+3)->getCourse() << endl;
The body of maximumGrade can then be e. g.
CourseGrade *c = NULL;
float g = 0; // current maximum
for (; course0 < course1; ++course0) if (g <= course0->getGrade())
g = (c = course0)->getGrade();
return c;

Related

Creating a function to output a string when the input is negative or zero. First time doing user-defined functions

I am trying to create a user-define function in C++ to prevent an endless loop from inputting an incorrect input for a double variable and check if an input is negative or zero. If that's the case the function will go into a do-while loop to ask the user to try again until the value is no longer something other than a double, negative, or zero.
The function fix() is the the user-defined
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
string fix(double x)
{
string B_error = "B cannot be zero or negative. Please try again: ";
string H_error = "H cannot be zero or negative. Please try again: ";
string h_error = "b cannot be zero or negative. Please try again: ";
string b_error = "h cannot be zero or negative. Please try again: ";
string r_error = "r cannot be zero or negative. Please try again: ";
string y_error;
while (!(cin >> x))
{
if (cin.fail())
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not an integer
cin.ignore(10000, '\n');
}
}
if (x == 'B')
{
y_error = B_error;
if (x <= 0)
{
do
{
return y_error;
cin >> x;
}
while (x <= 0);
}
}
return 0;
}
int main()
{
int selection;
double I, B, H, b, h, r, fix(double);
cout << "Please select the type of beam:\n"
<< "1) I-Beam\n"
<< "2) Rectangular Beam\n"
<< "3) Cylindrical Beam\n";
while (!(cin >> selection) || selection < 1 || selection > 3)
{
if (cin.fail() || selection < 1 || selection > 3)
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not an integer
cin.ignore(10000, '\n');
}
}
switch (selection)
{
case 1:
cout << "You have selected I-beam. All inputs must be in inches.\n"
<< "Please input the value for B: ";
fix(B);
cout << "Please input the value for H: ";
fix(H);
if (H <= 0)
{
do
{
cout << "H cannot be zero or negative. Please try again: ";
cin >> H;
}
while (H <= 0);
}
cout << "Please input the value for b: ";
fix(b);
if (b <= 0)
{
do
{
cout << "b cannot be zero or negative. Please try again: ";
cin >> b;
}
while (b <= 0);
}
else if (b > B)
{
do
{
cout << "b cannot be larger than B. Please try again: ";
cin >> b;
}
while (b > B);
}
cout << "Please input the value for h: ";
fix(h);
if (h <= 0)
{
do
{
cout << "h cannot be zero or negative. Please try again: ";
cin >> h;
}
while (h <= 0);
}
else if (h > H)
{
do
{
cout << "h cannot be larger than H. Please try again: ";
cin >> H;
}
while (h > H);
}
I = (B*H*H*H - b*h*h*h)/12.;
cout << "\nResults for an I-beam with B = " << B
<< ", H = " << H << ", b = " << b << ", and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 2:
cout << "You have selected rectangular beam. All inputs must be in inches.\n"
<< "Please input the value for b: ";
fix(b);
if (b <= 0)
{
do
{
cout << "b cannot be zero or negative. Please try again: ";
cin >> b;
}
while (b <= 0);
}
cout << "Please input the value for h: ";
fix(h);
if (h <= 0)
{
do
{
cout << "h cannot be zero or negative. Please try again: ";
cin >> h;
}
while (h <= 0);
}
I = b*h*h*h/12.;
cout << "\nResults for a rectangular beam with b = " << b << " and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 3:
cout << "You have selected cylindrical beam. All inputs must be in inches.\n"
<< "Please input the value of r: ";
fix(r);
if (r <= 0)
{
do
{
cout << "r cannot be zero or negative. Please try again: ";
cin >> r;
}
while (r <= 0);
}
I = M_PI*pow(r,4)/4.;
cout << "\nResults for a cylindrical beam with r = " << r << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
}
cout << "The value of the moment of inertia for this beam is: " << I << "in^4" << "\n\n";
return 0;
}
I removed the issues. You are confusing instances of classes with functions. Functions don't have to be initialized, instances have to if they are outside of the class.
I wrote some comments along the code. It is still not beautiful but at least it works.
string FUNCTION(double) btw. means the function only can or should return a "string". If you are returning nothing the function is written like so void FUNCTION(double).
If your return f.e. a string you have to write something that is receiving the returned string like so:
#include <iostream>
/*
std::string returning_value;
returning_value = FUNCTION(1.0);
*/
//or
std::string FUNCTION(double function_a); //prototype of the function
//you need this if you write the function underneath the main() function
//The main function is returning "return 0" so since "0" is an "int"
//meaning main is always "int main()" btw. because it is a function,
//just not some function but the "main function" thats called by the OS
int main()
{
std::string returning_value;
double a = 0.1; //initializing with 0.1
returning_value = FUNCTION(a);
std::cout << returning_value << std::endl;
//and in both cases the function would look like:
return 0;
}
std::string FUNCTION(double function_a)
{
std::string returning_value_a = "This is a string that will be returned";
if(function_a == 0.1)
{
returning_value_a = "This is another string";
}
return returning_value_a;
}
Your code with the least amount fixed that you got it at least working
and you can test with how to get the right output you want to get. Have fun :) Hope my answer helps you :)
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void fix(double x)
{
string B_error = "B cannot be zero or negative. Please try again: ";
string H_error = "H cannot be zero or negative. Please try again: ";
string h_error = "b cannot be zero or negative. Please try again: ";
string b_error = "h cannot be zero or negative. Please try again: ";
string r_error = "r cannot be zero or negative. Please try again: ";
string y_error;
while (!(cin >> x))
{
if (cin.fail())
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not an integer
cin.ignore(10000, '\n');
}
}
//warning: comparing floating point with == or != is unsafe
if (x == 'B')
{
y_error = B_error;
if (x <= 0)
{
do
{
cin >> x;
}
while (x <= 0);
}
}
}
int main()
{
int selection;
double I, B, H, b, h, r;
//You need to initialize the variables with a value
I = 1.0;
B = 1.0;
H = 1.0;
b = 1.0;
h = 1.0;
r = 1.0;
//functions don't need to be initialized, thats for Instanzes of classes
//double fix(double);
cout << "Please select the type of beam:\n"
<< "1) I-Beam\n"
<< "2) Rectangular Beam\n"
<< "3) Cylindrical Beam\n";
while (!(cin >> selection) || selection < 1 || selection > 3)
{
if (cin.fail() || selection < 1 || selection > 3)
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not an integer
cin.ignore(10000, '\n');
}
}
switch (selection)
{
case 1:
cout << "You have selected I-beam. All inputs must be in inches.\n"
<< "Please input the value for B: ";
fix(B);
cout << "Please input the value for H: ";
fix(H);
if (H <= 0)
{
do
{
cout << "H cannot be zero or negative. Please try again: ";
cin >> H;
}
while (H <= 0);
}
cout << "Please input the value for b: ";
fix(b);
if (b <= 0)
{
do
{
cout << "b cannot be zero or negative. Please try again: ";
cin >> b;
}
while (b <= 0);
}
else if (b > B)
{
do
{
cout << "b cannot be larger than B. Please try again: ";
cin >> b;
}
while (b > B);
}
cout << "Please input the value for h: ";
fix(h);
if (h <= 0)
{
do
{
cout << "h cannot be zero or negative. Please try again: ";
cin >> h;
}
while (h <= 0);
}
else if (h > H)
{
do
{
cout << "h cannot be larger than H. Please try again: ";
cin >> H;
}
while (h > H);
}
I = (B*H*H*H - b*h*h*h)/12.;
cout << "\nResults for an I-beam with B = " << B
<< ", H = " << H << ", b = " << b << ", and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 2:
cout << "You have selected rectangular beam. All inputs must be in inches.\n"
<< "Please input the value for b: ";
fix(b);
if (b <= 0)
{
do
{
cout << "b cannot be zero or negative. Please try again: ";
cin >> b;
}
while (b <= 0);
}
cout << "Please input the value for h: ";
fix(h);
if (h <= 0)
{
do
{
cout << "h cannot be zero or negative. Please try again: ";
cin >> h;
}
while (h <= 0);
}
I = b*h*h*h/12.;
cout << "\nResults for a rectangular beam with b = " << b << " and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 3:
cout << "You have selected cylindrical beam. All inputs must be in inches.\n"
<< "Please input the value of r: ";
fix(r);
if (r <= 0)
{
do
{
cout << "r cannot be zero or negative. Please try again: ";
cin >> r;
}
while (r <= 0);
}
I = M_PI*pow(r,4)/4.;
cout << "\nResults for a cylindrical beam with r = " << r << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
}
cout << "The value of the moment of inertia for this beam is: " << I << "in^4" << "\n\n";
return 0;
}
So, I had to add another function to check if B < b and H < h and add a do-while loop after the while loop in the fix function.
Here's my code with the fix:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void fix(double &x)
{
while (!(cin >> x))
{
if (cin.fail())
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not a double
cin.ignore(10000, '\n');
}
}
string return_x = "This cannot be zero or negative. Please try again: ";
while (x <= 0)
{
cout << return_x << endl;
cin >> x;
}
}
void fix2(double &x, double &y)
{
while (x < y)
{
cout << x << " cannot be less than " << y << endl;
fix(y);
}
}
int main()
{
int selection;
double I = 1.0;
double B = 1.0;
double H = 1.0;
double b = 1.0;
double h = 1.0;
double r = 1.0;
cout << "Please select the type of beam:\n"
<< "1) I-Beam\n"
<< "2) Rectangular Beam\n"
<< "3) Cylindrical Beam\n";
while (!(cin >> selection) || selection < 1 || selection > 3)
{
if (cin.fail() || selection < 1 || selection > 3)
{
cout << "Erroneous input. Please try again:\n";
cin.clear(); // used to prevent an endless loop if an input type is not an integer
cin.ignore(10000, '\n');
}
}
switch (selection)
{
case 1:
cout << "You have selected I-beam. All inputs must be in inches.\n"
<< "Please input the value for B: ";
fix(B);
cout << "Please input the value for H: ";
fix(H);
cout << "Please input the value for b: ";
fix(b);
fix2(B, b);
cout << "Please input the value for h: ";
fix(h);
fix2(H, h);
I = (B * H * H * H - b * h * h * h) / 12.;
cout << "\nResults for an I-beam with B = " << B << ", H = " << H
<< ", b = " << b << ", and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 2:
cout << "You have selected rectangular beam. All inputs must be in inches.\n"
<< "Please input the value for b: ";
fix(b);
cout << "Please input the value for h: ";
fix(h);
I = b * h * h * h / 12.;
cout << "\nResults for a rectangular beam with b = " << b
<< " and h = " << h << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
case 3:
cout << "You have selected cylindrical beam. All inputs must be in inches.\n"
<< "Please input the value of r: ";
fix(r);
I = M_PI * pow(r, 4) / 4.;
cout << "\nResults for a cylindrical beam with r = " << r << endl;
cout << setfill('-') << setw(32) << "" << endl;
break;
}
cout << "The value of the moment of inertia for this beam is: " << I
<< "in^4" << "\n\n";
return 0;
}

Saving all Inputs entered for display on cout - C++

I have looked in the forum but can't seem to find anything specific to what I need.
I am writing a program that asks the user to input a number of students. Depending on the number of students they enter they will then have to enter the students name and a series of 10 grades, or pressing 999 to cancel. The program will later have to display all students entered with their average grade. What I have now just overrides the previous inputs and displays the last one entered.
This is what I have so far:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
int main() {
std::string teacherName = "";
std::string classDesignation ="";
int numStudents = 0;
std::string studentName = "";
double grade[10];
double averageGrade = 0.00;
char letterGrade;
std::cout << "Enter the teacher's name: ";
getline(std::cin, teacherName);
std::cout << "Enter the class designation: ";
getline(std::cin, classDesignation);
std::cout << "Enter the number of students ( 1 or more ): ";
std::cin >> numStudents;
std::cin.ignore();
for (int x = 0; x <= numStudents - 1; x++) {
std::cout << "Enter the student's name: ";
getline(std::cin, studentName);
for (int i = 0; i <= 9; i++) {
std::cout << "Enter grade from 0 - 100 or 999 to stop: ";
std::cin >> grade[i];
if (grade[i] == 999){
break;
}
averageGrade += grade[i];
if (averageGrade <= 59){
letterGrade = 'F';
}
if (averageGrade >= 60 || averageGrade <= 69){
letterGrade = 'D';
}
if (averageGrade >= 70 || averageGrade <= 79){
letterGrade = 'C';
}
if (averageGrade >= 80 || averageGrade <= 89){
letterGrade = 'B';
}
if (averageGrade > 90){
letterGrade = 'A';
}
}
}
std::cout << "Teacher: " << teacherName << std::endl;
std::cout << "Class: " << classDesignation << std::endl;
std::cout << "Student Name: " << studentName;
std::cout << std::setw(19) << "Average: " << averageGrade;
std::cout << " Grade: " << letterGrade << std::endl;
std::cout << "Student count: " << numStudents << std::endl;
std::cout << "Student average: " << std::endl;
std::cout << "A's: " << std::endl;
std::cout << "B's: " << std::endl;
std::cout << "C's: " << std::endl;
std::cout << "D's: " << std::endl;
std::cout << "F's: " << std::endl;
return 0;
}
Any tips?
Thank you!
#Hansel, here's what I think will work. We'll change this line to get what we want:
averageGrade += grade[i];
To:
//to declarations:
double averageGradeSum = 0.00;
//then change the line mentioned before to:
averageGradeSum += grade[i];
Then:
//if for some reason you're indexing starts at 1
averageGrade = averageGradeSum/i;
//if indexing starts at 0 like usual C++ use:
averageGrade = averageGradeSum/(i+1);
I'm not going to test this. It should make sense and if I screwed up the syntax it should be an easy google adventure to fix. Enjoy :)
From what I see, you can store them in associative vectors, one for the student name, and one for the average grade. You can also have a doubly linked list of student nodes which would look something like this:
struct student
{
std::string student_name;
int ave_grade;
// Head points to the previous student in the array.
// Tail points to the next student in the array.
student *head;
student *tail;
}
Declare a struct that everything hangs from to prevent memory leaks: student list_head; and each time you have an input, add a new node to the list.

Program to calculate NFL quarterback passer rating. Why is it returning 0?

Hey so the code I made should be working to calculate the passer rating for quarterbacks in the NFL. The program, however, returns a value of 0 for almost anything, unless I put ridiculously large numbers, in which case it gives 100. What's wrong with it?
#include <iostream>
using namespace std;
int main()
{
int PassCompletions;
cout << "Enter pass completions" << endl;
cin >> PassCompletions;
int PassAttempts;
cout << "Enter pass attempts" << endl;
cin >> PassAttempts;
int TotalPassY;
cout << "Enter total yards" << endl;
cin >> TotalPassY;
int Touch;
cout << "Enter touchdowns" << endl;
cin >> Touch;
int Int;
cout << "Enter interceptions" << endl;
cin >> Int;
int C = (PassCompletions/PassAttempts-0.30)*5;
int Y = (TotalPassY/PassAttempts-3)*0.25;
int T = (Touch/PassAttempts)*20;
int I = 2.375 - (Int/PassAttempts*25);
if (C<0){
C=0;
}
if (Y<0){
Y=0;
}
if (T<0){
T=0;
}
if (I<0){
I=0;
}
if (C>2.375){
C=2.375;
}
if (Y>2.375){
Y=2.375;
}
if (T>2.375){
T=2.375;
}
if (I>2.375){
I=2.375;
}
int PasserRating = (C+Y+T+I)/6*100;
if (PasserRating <= 85){
cout << "Rating " << PasserRating << ", this is poor" << endl;
}
if (PasserRating > 85 && PasserRating < 90){
cout << "Rating " << PasserRating << ", this is mediocre" << endl;
}
if (PasserRating > 90 && PasserRating < 95){
cout << "Rating " << PasserRating << ", this is good" << endl;
}
if (PasserRating > 95){
cout << "Rating " << PasserRating << ", this is great" << endl;
}
You need use data type which is suitable to store fractional value. For this purpose use float instead of int for these statements:
float C = (PassCompletions/PassAttempts-0.30)*5;
float Y = (TotalPassY/PassAttempts-3)*0.25;
float T = (Touch/PassAttempts)*20;
float I = 2.375 - (Int/PassAttempts*25);
The variable type int is only used to store whole numbers, eg 1,2,3...
Any expression with a decimal will be truncated and rounded down. Since you are doing a lot of calculations with floating point numbers, eg. 2.375, I would suggest you changing your int's to float's

If statments are always executed

Help
#include <iostream>
#include <math.h>
using namespace std;
const float pi = 3.14;
void Odabir(int);
int main(){
int choose;
cout << "Odaberite 1 2 ili 3" << endl;
cin >> choose;
Odabir(choose);
return 0;
}
void Odabir(int choose){
if (choose = 1){
float b, vb;
cout << "Unesite duljinu stranice b: " << endl;
cin >> b;
cout << "Unesite duljinu visine na stranicu b vb: " << endl;
cin >> vb;
cout << "Povrsina raznostranicnog trokuta je: " << ((b*vb) / 2) << endl;
}
if (choose = 2){
float r;
cout << "Unesite duljinu polumjera: " << endl;
cin >> r;
cout << "Povrsina kruga je: " << pow(r, 2)*pi << endl;
}
}
I have been trying to solve that for few hours and I can't get thru it, it looks like when I set choose value all three if clauses get executed and printed.
You are using the assignment operator ( = ) instead of the comparison operator ( == ) in the if statements in the function.
For example
if (choose = 1){
^^^
Write instead
if (choose == 1){
^^^^
You are assigning the variable choose instead of comparing it inside the Odabir function.
for instance, choose = 1 should be choose == 1

C++ Output Errors

The output for this program, thanks to you guys, is fixed. Except for the studentNumber. I read the comment that I never set a value to it and that confused me.
void process_file(ifstream& input)
{
int thisStudent = 0;
StudentRecord student = StudentRecord();
while (thisStudent++ < CLASS_SIZE)
{
student.input(input, thisStudent);
student.computeGrade();
student.output();
}
}
would this not set studentNumber equal to 0 then add +1 every time it runs through the loop.
// Author:
// Assignment 8
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35;
const int CLASS_SIZE = 5;
class StudentRecord
{
public:
void input( ifstream& input,int studentid);
void computeGrade();
void output();
private:
int studentNumber;
double exam1;
double exam2;
double exam3;
double exam4;
double average;
char grade;
};
void open_input(ifstream& input, char name[]);
void process_file(ifstream& input);
int main()
{ char again;
char file_name[MAX_FILE_NAME + 1];
ifstream input_numbers;
cout << "This program can calculate the exam average and grade for\n"
<< "each student.\n" << endl;
system("pause");
do
{
system("cls");
open_input(input_numbers, file_name);
process_file(input_numbers);
input_numbers.close();
cout << "\nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(256, '\n');
} while ( again == 'y' || again == 'Y');
cout << "\nEnd of Program!" << endl;
outputfile << "\n\nThanks for using GradeCalc!\f";
outputfile.close();
return 0;
}
void process_file(ifstream& input)
{
int thisStudent = 0;
StudentRecord student = StudentRecord();
while (thisStudent++ < CLASS_SIZE)
{
student.input(input, thisStudent);
student.computeGrade();
student.output();
}
}
void open_input(ifstream& input, char name[])
{ int count = 0;
do
{ count++;
if (count != 1)
{ cout << "\n\aInvalid file name or file does not exist. Please try again."
<< endl;
}
cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)\n:> ";
cin.get(name, MAX_FILE_NAME + 1);
cin.ignore(256, '\n');
input.clear();
input.open(name,ios_base::in);
} while (input.fail() );
}
void StudentRecord::input(ifstream& input, int studentid)
{
input >> exam1 >> exam2 >> exam3 >> exam4;
}
void StudentRecord::computeGrade()
{
average = (exam1 + exam2 + exam3 + exam4) / 4 ;
if (average >= 90)
grade = 'A';
else if (average >= 80)
grade = 'B';
else if (average >= 70)
grade = 'C';
else if (average >= 60)
grade = 'D';
else if (average < 60)
grade = 'F';
}
void StudentRecord::output()
{
cout << "\n\nThe record for student number:" << setw(8) << studentNumber << endl;
cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
cout << "The numeric average is:" << setw(8) << average << endl;
cout << "and the letter grade assigned is:" << setw(8) << grade << endl;
}
Well, studentNumber is garbage because you never put a value in it. So it just has whatever happened to already be in memory at that location.
The exam grades print out wrong because commas in C++ don't do what you think they do, and that's also why adding an endl; to it gives you an error.
The formatting I'm going to let you work out for yourself. You should consider reading up on output or at least doing some trial and error.
One of the errors is that instead of this:
cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;
I think you mean this:
cout << "The exam grades are:" << setw(8) << exam1 << exam2 << exam3 << exam4 << endl;
CLASS_SIZE is defined as 5, so this loop:
while (thisStudent++ < CLASS_SIZE)
will iterate 6 times.
Also
cout << "The exam grades are:" << setw(8) << exam1, exam2, exam3, exam4;
This outputs exam1, and then evaluates and does nothing with the rest of the variables.
70 80 90 95 95 85 90 80 75 85 70 80 55 85 50 70 45 50 40 35
does it have the spaces? If yes, you need to ignore them. input >> exam1 >> exam2 >> exam3 >> exam4; would load space into one of the exam variables.
-- edit for MooingDuck --
#include <iostream>
#include <sstream>
using namespace std;
int main() {
cout << "main() ENTRY" << endl;
stringstream s1(ios_base::in | ios_base::out),
s2(ios_base::in | ios_base::out);
int i = -1;
s1 << "111 222";
s1 >> i; cout << i << endl;
s1 >> i; cout << i << endl;
s2 << "111 222";
s2 >> noskipws;
s2 >> i; cout << i << endl;
s2 >> i; cout << i << endl;
return 0;
}
Output:
main() ENTRY
111
222
111
0