Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

Similar documents
Files Total: // Files Example 1. #include <iostream> #include <fstream>

CSCI 1370 APRIL 26, 2017

System Design and Programming II

Input and Output File (Files and Stream )

CSCE 110 PROGRAMMING FUNDAMENTALS

Tutorial letter 202/1/2015

Tutorial letter 202/2/2018

BITG 1113: Files and Stream LECTURE 10

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Convenient way to deal large quantities of data. Store data permanently (until file is deleted).

CSC 138 Structured Programming CHAPTER 4: TEXT FILE [PART 1]

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

CSI33 Data Structures

Week 3: File I/O and Formatting 3.7 Formatting Output

CPE Summer 2015 Exam I (150 pts) June 18, 2015

CS 103 Unit 10 Slides

EECS 183 Week 8 Diana Gage. www-personal.umich.edu/ ~drgage

LAB 7.1 Working with One-Dimensional Arrays

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

This test is OPEN Textbook and CLOSED notes. The use of computing and/or communicating devices is NOT permitted.

Name Section: M/W T/TH Number Definition Matching (8 Points)

2. It is possible for a structure variable to be a member of another structure variable.

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

CSCE Practice Midterm. Data Types

CSCE 206: Structured Programming in C++

Notes on the 2016 Exam

CSC1322 Object-Oriented Programming Concepts

Chapter void Test( int, int, int ); // Function prototype int main() // Function heading { int h; // Local variable

Text File I/O. #include <iostream> #include <fstream> using namespace std; int main() {

Name Section: M/W T/TH Number Definition Matching (6 Points)

Introducing C++ to Java Programmers

Linked List using a Sentinel

Lecture 3 The character, string data Types Files

CS 103 Unit 10 Slides

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

CMSC 202 Midterm Exam 1 Fall 2015

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

Review Problems for Final Exam. 1. What is the output of the following program? #include <iostream> #include <string> using namespace std;

Page 1 of 5. *** person.cpp *** #include "person.h" #include <string> #include <iostream> using namespace std; *** person.h ***

Programming II (CS300)

C++ STREAMS; INHERITANCE AS

PROGRAMMING EXAMPLE: Checking Account Balance

lecture04: Constructors and Destructors

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

C++ Input/Output Chapter 4 Topics

Summary of basic C++-commands

Objects and streams and files CS427: Elements of Software Engineering

System Design and Programming II

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Structures. e.g., Smith Mary F brown 27 lawyer

Chapte t r r 9

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

C++ Mini Lessons Last Update: Feb 7, 2018

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Chapter 10 RECORDS (Structs )

CS 115 Exam 3, Spring 2010

Advanced File Operations. Review of Files. Declaration Opening Using Closing. CS SJAllan Chapter 12 2

Object Oriented Programming COP3330 / CGS5409

CSCE Practice Midterm. Data Types

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

First Exam Computer Programming 326 Dr. St. John Lehman College City University of New York Thursday, 7 October 2010

Multiple Choice Questions (20 questions * 6 points per question = 120 points)

ECE 462 Exam 1. 6:30-7:30PM, September 22, 2010

File I/O Christian Schumacher, Info1 D-MAVT 2013

Multiple Choice Questions (20 questions * 5 points per question = 100 points)

File handling Basics. Lecture 7

Streams in C++ Stream concept. Reference information. Stream type declarations

C++ does not, as a part of the language, define how data are sent out and read into the program

Review Questions for Final Exam

Developed By : Ms. K. M. Sanghavi

Definition Matching (10 Points)

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

COL 100. Minor 2 Exam - Practice

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 7. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

COMP322 - Introduction to C++

pointers & references

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Sample Code: OUTPUT Daily Highs & Lows

File I/O. File Names and Types. I/O Streams. Stream Extraction and Insertion. A file name should reflect its contents

Assignment 2 Solution

This sheet must be the cover page for every programming assignment. Total

PIC 10A. Lecture 17: Classes III, overloading

Multiple Inheritance and Object Layout in C++

CSCE 110 PROGRAMMING FUNDAMENTALS

A SHORT COURSE ON C++

Simple File I/O.

CS 141, Introduction to Computer Science Fall Midterm Exam

Chapter 3 - Notes Input/Output

Week 5: Files and Streams

I/O Streams and Standard I/O Devices (cont d.)

การทดลองท 8_2 Editor Buffer Array Implementation

PIC 10A. Final Review: Part I

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 11: Records (structs)

Fundamentals of Programming Session 28

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Transcription:

Class Example student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED #include <string> #include <fstream> using namespace std; class student public: student(); student(string first, string last, long stuid); ~student(); void setfirstname(string first); void setlastname(string last); void setid(long stuid); string getfirstname(); string getlastname(); long getid(); double getaverage(); int getnumberofgrades(); char getlettergrade(); void addexamscore(double score); void calculateaverage(int numexams); void calculateaverageoftakenexams(); void assignlettergarde(); void printstudentinformation(); void printstudentinformationtofile(ofstream &outfile); private: string firstname; string lastname; long studentid; double examscores[10]; int numgrades; double averageoftakenexams; // average of exams taken. double average; // average of all exams. char lettergrade; ; #endif // STUDENT_H_INCLUDED 1

student.cpp file: Implementation of the student template. #include <iostream> #include "student.h" using namespace std; // Constructors student::student() firstname = ""; lastname = ""; studentid = 0; numgrades = 0; student::student(string first, string last, long stuid) firstname = first; lastname = last; studentid = stuid; numgrades = 0; // Destructor student::~student() // Accessor Functions void student::setfirstname(string first) firstname = first; void student::setlastname(string last) lastname = last; void student::setid(long stuid) studentid = stuid; string student::getfirstname() return firstname; string student::getlastname() return lastname; 2

long student::getid() return studentid; double student::getaverage() return average; int student::getnumberofgrades() return numgrades; char student::getlettergrade() return lettergrade; void student::addexamscore(double score) if (numgrades < 10) examscores[numgrades] = score; numgrades++; void student::calculateaverage(int numexams) double totalscore = 0; for (int j = 0; j < numgrades; j++) totalscore += examscores[j]; if (numexams > 0) average = totalscore/numexams; else average = 0; void student::calculateaverageoftakenexams() double totalscore = 0; for (int j = 0; j < numgrades; j++) totalscore += examscores[j]; if (numgrades > 0) averageoftakenexams = totalscore/numgrades; else averageoftakenexams = 0; 3

void student::assignlettergarde() if (average >= 90) lettergrade = 'A'; else if (average >= 80) lettergrade = 'B'; else if (average >= 70) lettergrade = 'C'; else if (average >= 60) lettergrade = 'D'; else lettergrade = 'F'; void student::printstudentinformation() cout << lastname << ", " << firstname << endl; cout << "Number of Exams Taken: " << numgrades << endl; cout << "Exam Scores: "; for (int j = 0; j < numgrades; j++) cout << examscores[j] << " "; cout << endl; cout << "Average of Exams Taken: " << averageoftakenexams << endl; cout << "Average: " << average << endl; cout << "Letter Grade: " << lettergrade << endl << endl; void student::printstudentinformationtofile(ofstream &outfile) outfile << lastname << ", " << firstname << endl; outfile << "Number of Exams Taken: " << numgrades << endl; outfile << "Exam Scores: "; for (int j = 0; j < numgrades; j++) outfile << examscores[j] << " "; outfile << endl; outfile << "Average of Exams Taken: " << averageoftakenexams << endl; outfile << "Average: " << average << endl; outfile << "Letter Grade: " << lettergrade << endl << endl; 4

main.cpp file: #include <iostream> #include <fstream> #include <string> #include "student.h" using namespace std; int SearchID(student mathclass[], int size, long targetid) if (mathclass[i].getid() == targetid) return i; return -1; void SortClassByLastName(student mathclass[], int size) bool swap; student temp; int bottom = size - 1; do swap = false; for (int count = 0; count < bottom; count++) if (mathclass[count].getlastname().compare(mathclass[count+1].getlastname()) > 0) temp = mathclass[count]; mathclass[count] = mathclass[count+1]; mathclass[count+1] = temp; swap = true; bottom--; while(swap!= false); void LoadIntoClass(student mathclass[], int &size, student stu, double examscore) int pos = SearchID(mathclass, size, stu.getid()); if (pos >= 0) mathclass[pos].addexamscore(examscore); else stu.addexamscore(examscore); mathclass[size] = stu; size++; 5

void CalculateStudentAveragesAndLetterGrades(student mathclass[], int size) int maxexams = 0; mathclass[i].calculateaverageoftakenexams(); if (mathclass[i].getnumberofgrades() > maxexams) maxexams = mathclass[i].getnumberofgrades(); mathclass[i].calculateaverage(maxexams); mathclass[i].assignlettergarde(); double CalculateClassAverage(student mathclass[], int size) double totalscore = 0; totalscore += mathclass[i].getaverage(); if (size > 0) return totalscore/size; else return 0; void PrintClassReport(student mathclass[], int size) cout << "Student Grade Reports" << endl << endl; mathclass[i].printstudentinformation(); cout << "Class Report" << endl << endl; cout << "Number of students in the class: " << size << endl; double classavg = CalculateClassAverage(mathclass, size); cout << "Class Average: " << classavg << endl << endl; cout << "Grade Distribution" << endl << endl; for (char ch = 'A'; ch <= 'F'; ch++) if (ch!= 'E') cout << ch << ": "; if (mathclass[i].getlettergrade() == ch) cout << "*"; cout << endl; 6

void PrintClassReportToFile(student mathclass[], int size, string filename) ofstream outfile; outfile.open(filename.c_str()); outfile << "Student Grade Reports" << endl << endl; mathclass[i].printstudentinformationtofile(outfile); outfile << "Class Report" << endl << endl; outfile << "Number of students in the class: " << size << endl; double classavg = CalculateClassAverage(mathclass, size); outfile << "Class Average: " << classavg << endl << endl; outfile << "Grade Distribution" << endl << endl; for (char ch = 'A'; ch <= 'F'; ch++) if (ch!= 'E') outfile << ch << ": "; if (mathclass[i].getlettergrade() == ch) outfile << "*"; outfile << endl; outfile.close(); int main() student mathclass[20]; int mathclasssize = 0; char ch; ifstream datafile; datafile.open("datafile.txt"); if(!datafile) cout << "Error opening file.\n"; cin.ignore(); return 1; 7

while ((ch = datafile.peek())!= EOF) double examscore; string first; string last; long ID; datafile >> first; datafile >> last; datafile >> ID; datafile >> examscore; student stu(first, last, ID); LoadIntoClass(mathclass, mathclasssize, stu, examscore); datafile.close(); CalculateStudentAveragesAndLetterGrades(mathclass, mathclasssize); SortClassByLastName(mathclass, mathclasssize); PrintClassReport(mathclass, mathclasssize); cout << endl; char choice; do cout << "Would you like to save the class information to a file? (Y/N): "; cin >> choice; cin.ignore(255, '\n'); while (toupper(choice)!= 'Y' && toupper(choice)!= 'N'); if (toupper(choice) == 'Y') string filename; cout << "Input Filename: "; getline(cin, filename); PrintClassReportToFile(mathclass, mathclasssize, filename); return 0; 8

Output: Student Grade Reports Anderson, Kevin Number of Exams Taken: 4 Exam Scores: 53 65 74 60 Average of Exams Taken: 63 Average: 50.4 Letter Grade: F Black, Joe Number of Exams Taken: 5 Exam Scores: 76 81 65 89 93 Average of Exams Taken: 80.8 Average: 80.8 Letter Grade: B Doe, John Number of Exams Taken: 3 Exam Scores: 84 23 13 Average of Exams Taken: 40 Average: 24 Letter Grade: F Duncan, Jane Number of Exams Taken: 5 Exam Scores: 91 92 77 90 75 Average of Exams Taken: 85 Average: 85 Letter Grade: B Smith, Rita Number of Exams Taken: 5 Exam Scores: 68 54 61 72 68 Average of Exams Taken: 64.6 Average: 64.6 Letter Grade: D Thomas, Jackie Number of Exams Taken: 5 Exam Scores: 86 72 86 86 71 Average of Exams Taken: 80.2 Average: 80.2 Letter Grade: B Zimmermann, Adam Number of Exams Taken: 5 Exam Scores: 79 82 89 88 82 Average of Exams Taken: 84 Average: 84 Letter Grade: B 9

Class Report Number of students in the class: 7 Class Average: 67 Grade Distribution A: B: **** C: D: * F: ** Would you like to save the class information to a file? (Y/N): y Input Filename: testout.txt 10