I'm having trouble with a homework assignment.
Everything works as expected except for the letter_grade part. I'm not getting any compile errors, but every time I run the program, the letter_grade is -858993460. I know it's probably something simple that I am overlooking, but I've hit a wall and am pretty much out of ideas.
#include <iostream>
#include <string>
using namespace std;
// Class Declaration
class StudentRecord
{
string id;
int A, B, C, D, F, grade;
float avg, exam1, exam2;
public:
void input();
void output();
void average();
void letter_grade();
};
// Gathering data
void StudentRecord::input()
{
cout << " Please enter your student ID: ";
cin >> id;
cout << "\n";
cout << " Enter your score for exam 1: ";
cin >> exam1;
cout << "\n";
cout << " Enter your score for exam 2: ";
cin >> exam2;
}
// Calculations
void StudentRecord::average()
{
avg = (exam1 + exam2) / 2;
}
void StudentRecord::letter_grade()
{
if ((avg >= 90) && (avg <= 100))
grade = A;
else if ((avg >= 80) && (avg < 90))
grade = B;
else if ((avg >= 70) && (avg < 80))
grade = C;
else if ((avg >= 60) && (avg < 70))
grade = D;
else (avg < 60);
grade = F;
}
// Output Data
void StudentRecord::output()
{
cout << "\n\n";
cout << " *** Student Record ***" << endl;
cout << "\n";
cout << " Student ID: " << id << endl;
cout << "\n";
cout << " Grade for exam 1: " << exam1 << endl;
cout << "\n";
cout << " Grade for exam 2: " << exam2 << endl;
cout << "\n";
cout << " Average for the class: " << avg << endl;
cout << "\n";
cout << " Letter grade for the class: " << grade << endl;
cout << "\n\n";
}
int main()
{
StudentRecord student;
student.input();
student.average();
student.letter_grade();
student.output();
system ("pause");
return 0;
}
You've declared integer variables named A, B, C, D, etc. That's not really what you want here. You want to declare grade as a char variable. You want to put the character 'A', 'B', etc. in that char variable.
Example:
grade = 'A';
That puts the character 'A' into the variable grade.
If you remove those variables A...F, change grade to be of type char, and then rewrite your if-else statement to assigned letters 'A', 'B', etc. to grade, that should do the trick.
Take note of those single quotes around the characters. Those tell C++ that the thing between the quotes is a character constant.
You are defining the "grade" values , A,B,C,D,F as integers. They are never initialized, so contain garbage in them. I think your intent was to print out a letter grade. In that case, you need to represent the values as strings, not numbers. Something like this:
char *A = "A";
char *B = "B";
etc.
The declaration of a variable is simply a label to identify a memory location ( simplified ). So, the name of the variable has no correlation to its value.
Related
int samuelt1(){
ela = 80;
socials = 80;
total = ela + socials;
int grade = total / 11.5;
cout << "Samuel's grade average for term 1 is: " << grade << "%" << endl;
cout << "Individual Subjects: " << endl;
cout << "ELA: " << ela << endl;
cout << "Socials: " << socials << endl;
return grade;
}
int average(){
int avg = [samuelt1's grade??] / 1;
cout << avg;
return 0;
}
I'd like to pass the grade variable over to the average function; is there a way to do that? Thanks!
In the main function, store the value returned by the function samuelt1 in the variable Grade. Pass Grade as a parameter to the function average, like so
#include <iostream>
using namespace std;
int samuelt1(){
int ela = 80;
int socials = 80;
int total = ela + socials;
int grade = total / 11.5;
cout << "Samuel's grade average for term 1 is : " << grade << "%" << endl;
cout << "Individual Subjects : " << endl;
cout << "ELA : " << ela << endl;
cout << "Socials : " << socials << endl;
return grade;
}
int average(int grade){
int avg = grade / 1;
cout << "Average : " << avg;
return 0;
}
int main(){
int Grade = samuelt1();
average(Grade);
}
In C++, as in most languages, functions can be created to accept parameters and/or return values. To do this, you just need to specify a type and a name inside the parenthesis. Here is an example:
void printGrade(int grade)
{
cout << grade << endl;
}
int main()
{
int grade = 10;
printGrade(grade);
}
Running that program will print 10 to the screen. One thing to note is that when you pass in an integer parameter, the computer is just creating a copy of the value. This means the original grade variable is not changed or affected. Consider this example:
void printGrade(int grade)
{
cout << grade << endl;
grade = 15;
}
int main()
{
int grade = 10;
printGrade(grade);
cout << grade << endl;
}
You may expect this program to print 10, followed by 15. Since a copy of value is created inside the printGrade() function, the value of grade is affected only inside the scope of the printGrade() function. If you need to change the original value inside the printGrade() function, then you must pass the parameters by reference. Here is an example:
void printGrade(int &grade)
{
cout << grade << endl;
grade = 15;
}
int main()
{
int grade = 10;
printGrade(grade);
cout << grade << endl;
}
You'll notice in this example, the variable name is preceded with an ampersand. This tells the computer that you want to pass a reference to the grade variable to the function rather than making a copy.
Hopefully this all makes sense!
#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;
So I am working on this program, and i have it mostly complete. I am just having trouble with running the loop again. If i run the loop again, it messes up by displaying both of the questions in the function at the same time.
And after the program is finished, i need to display all of the iterations at the same time.
// This program will gather the information for the quizzes that have been taken in a course to date
and // output the information into a report on the screen.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function Prototypes
void getQuiz(string&, int&, int&);
int main()
{
char grade;
char ans = 'Y';
string name;
int quiz_number = 0;
int score;
int poss_score;
int counter = 1;
double percentage;
// Explain what the program will do
cout << "This program will ask the user to enter the name of a quiz, the score aquired, and the
total possible score.\n";
cout << "It will display a report that will show the quiz number, the name, the points, total,
percentage, and the letter grade." << endl << endl;
do {
// Display count
cout << counter;
cout << endl << endl;
// Call function
getQuiz(name, score, poss_score);
// Accumulate the number of times the loop is run
counter++;
cout << "Would you like to add another quiz? (Y) or (N)" << endl;
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << endl << endl;
// Calculate the percentage
percentage = score * 100 / poss_score;
// Get the letter grade
if (percentage <= 100 && percentage >= 90)
grade = 'A';
else if (percentage <= 89 && percentage >= 80)
grade = 'B';
else if (percentage <= 79 && percentage >= 70)
grade = 'C';
else if (percentage <= 69 && percentage >= 60)
grade = 'D';
else if (percentage <= 59)
grade = 'F';
// Display the results in a table
cout << setw(5) << " Quiz" << setw(20) << " Title" << setw(20) << " Points" << setw(10) << "
Total" << setw(10) << " Percent" << setw(10) << " Grade" << endl << endl;
cout << setw(5) << counter << setw(20) << name << setw(20) << score << setw(10) << poss_score <<
setw(10) << percentage << setw(10) << grade << endl;
return 0;
}
void getQuiz(string& name, int& score, int& poss_score)
{
// Get the name of the quiz
cout << "\nWhat is the name of the quiz? " << endl;
getline(cin, name);
cout << "\nWhat was the score aquired? " << endl;
cin >> score;
cout << "\nWhat was the total possible score acheivable? " << endl;
cin >> poss_score;
}
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.
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