in Visual C++, Assignment learning objectives: You are supposed to correctly def
ID: 3732239 • Letter: I
Question
in Visual C++,
Explanation / Answer
//main.cpp
#include "Student.h"
using namespace std;
void main()
{
ifstream in("Students.txt");
ifstream rin("checkouts.txt");
ifstream lin("checkins.txt");
ofstream out("Updated_Students.txt");
unsigned int studentID;
string item;
int totalStudents = 0;
if (!in)
cout << "failed to open file.";
if (!rin)
cout << "failed to open file.";
if (!lin)
cout << "failed to open file.";
int i = 0;
const int size = 100;
Student s1;
Student* students = new Student[size];
while (in >> s1)
{
cout << s1;
}
//while (!in.eof())
//{
// in >> students[totalStudents];
// ++totalStudents;
//}
//for (int i = 0; i < totalStudents; i++)
//{
// students[i] = s1;
//}
in.close();
while (rin.good())
{
rin >> studentID;
for (int i = 0; i < totalStudents; i++)
{
if (students[i].getID() == studentID)
{
rin >> item;
students[i].CheckOut(item);
}
}
}
while (lin.good())
{
lin >> item;
for (int i = 0; i < totalStudents; i++)
{
if (students[i].CheckIn(item))
break;
}
}
for (int i = 0; i < totalStudents; i++)
out << students[i];
delete[] students;
students = NULL;
in.close();
rin.close();
lin.close();
out.close();
}
---------------------------------------------------------------------------------------------
//student.cpp
#include "Student.h"
Student::Student()
{
idNumb = 0;
firstName = "";
lastName = "";
itemName = "";
aSize = 0;
numItems = 0;
pointy = NULL;
}
void Student::setID(unsigned int nID)
{
if ((nID > 999) && (nID < 100001))
idNumb = nID;
else
cout << "Invalid ID Number.";
}
void Student::setFirst(string nFirst)
{
firstName = nFirst;
}
void Student::setLast(string nLast)
{
lastName = nLast;
}
int Student::CheckoutCount()
{
return numItems;
}
bool Student::CheckOut(const string& item)
{
if (HasCheckedOut(item))
return false;
if (pointy == NULL && aSize == 0)
{
string *newPointy = new string[5];
}
string *newPointy = new string[aSize];
for (int i = 0; i < aSize; i++)
{
newPointy[i] = pointy[i];
}
if (pointy != NULL && aSize > 0)
delete[] pointy;
pointy = newPointy;
pointy[numItems] = itemName;
numItems++;
return true;
}
bool Student::CheckIn(const string& item)
{
if (!HasCheckedOut(item))
return false;
string *newPointy = new string[aSize];
int nArraySize = 0;
for (int i = 0; i <aSize; i++)
{
if (pointy[i] != item)
{
newPointy[nArraySize] = pointy[i];
nArraySize++;
}
}
delete[] pointy;
pointy = newPointy;
numItems--;
}
bool Student::HasCheckedOut(const string& item)
{
for (int i = 0; i < aSize; i++)
{
if (pointy[i] == item)
return true;
}
return false;
}
void Student::Clear()
{
idNumb = 0;
firstName = "";
lastName = "";
if((pointy != NULL) && (numItems > 0))
delete[] pointy;
pointy = NULL;
numItems = 0;
}
Student::~Student()
{
delete[] pointy;
pointy = NULL;
}
istream& operator>>(istream& in, Student& item)
{
item.Clear();
string itemOut;
unsigned int id;
string first, last;
in >>id >> first >> last >> item.aSize;
item.setID(id);
item.setFirst(first);
item.setLast(last);
for (int i = 0; i < item.aSize; i++)
{
in >> itemOut;
item.CheckOut(itemOut);
}
return in;
}
ostream& operator<<(ostream& out, const Student& item)
{
out << item.idNumb << " " << item.firstName << " " << item.lastName << endl;
out << item.numItems << endl;
for (int i = 0; i < item.numItems; i++)
{
out << item.pointy[i] << " ";
}
return out;
}
Student Student::operator+(Student &student1)
{
Student anotherStudent;
anotherStudent = student1;
anotherStudent.CheckOut(itemName);
return anotherStudent;
}
void Student:: operator+=(string& itemName)
{
CheckOut(itemName);
}
bool operator==(Student & s1, Student &s2)
{
if (s1.getID() == s2.getID())
return true;
else
return false;
}
----------------------------------------------------------
//student.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student
{
protected:
string firstName, lastName, itemName;
unsigned int idNumb;
int numItems, aSize;
string* pointy = NULL;
public:
//default constructor
Student();
//setters
void setID(unsigned int nID);
void setFirst(string nFirst);
void setLast(string nLast);
//getters
int getID() { return idNumb; }
string getFirst() { return firstName; }
string getLast() { return lastName; }
int CheckoutCount();
//returns number of items student has checked out
//takes a string parameter to describe an item
bool CheckOut(const string& item);
//checks if item is in if it is, checks it out
//takes a string parameter to describe an item
bool CheckIn(const string& item);
//checks if item is checked out and deletes the array if so. a successful check in
//returns true, if not it returns false.
//takes an item parameter
bool HasCheckedOut(const string& item);
//checks if item is on the students checked out list
void Clear();
//clears the objects data
//destructor
~Student();
//Operator overloads
friend istream& operator>>(istream& in, Student& item);
//reads data in from text stream
friend ostream& operator<<(ostream& out, const Student& item);
//outputs data into text file
Student operator+(Student &student1);
//overloads the addition function adds an item to a student and outputs a new user
void operator+=(string &itemName);
//another way to add an item to a student and check it out
bool friend operator==(const Student &item, const Student &item1);
//checks if two students have the same id number or not
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.