So here is my program. I have to make an object of type Student, then have the Student "check out" an item. And I am using an overloaded addition operator to make the user check out that item.
main.cpp:
#include <iostream>
#include "Student.h"
using namespace std;
int main() {
Student s(54000, "JOHN", "DOE");
cout << "main:" << endl << (s + "Frisbee") << endl << endl;
system("pause");
return 0;
}
I defined all my class defintions in the header file to try and keep this program minimal and simplified.
Student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class Student {
public:
string firstName;
string lastName;
int id;
int itemsCheckedOut;
int size;
string *array;
Student(int id = 0, string firstName = "", string lastName = "") {
Student::firstName = firstName;
Student::lastName = lastName;
Student::id = id;
itemsCheckedOut = 0;
size = 10;
array = new string[size];
}
Student(const Student &other) {
itemsCheckedOut = other.itemsCheckedOut;
array = new string[itemsCheckedOut];
for (int i = 0; i < itemsCheckedOut; i++) {
array[i] = other.array[i];
}
}
~Student() {
delete[] array;
array = NULL;
}
Student &operator=(const Student &rhs) {
if (this != &rhs) {
firstName = rhs.firstName;
lastName = rhs.lastName;
id = rhs.id;
itemsCheckedOut = rhs.itemsCheckedOut;
delete[] array;
array = new string[size];
for (int i = 0; i < itemsCheckedOut; i++) {
array[i] = rhs.array[i];
}
}
return *this;
}
void CheckOut(const string &item) {
array[itemsCheckedOut] = item;
itemsCheckedOut++;
}
friend ostream &operator<<(ostream &output, const Student &student) {
output << student.id << " " << student.firstName << " " << student.lastName << endl;
if (student.itemsCheckedOut != 0) {
output << student.itemsCheckedOut;
for (int i = 0; i < student.itemsCheckedOut; i++) {
output << " " << student.array[i] << endl;
}
}
else {
output << 0;
}
return output;
}
const Student operator+(const string &item) {
Student s;
s = *this;
s.CheckOut(item);
cout << "class:" << endl << s << endl << endl;
return s;
}
};
#endif
output:
class:
54000 JOHN DOE
1 Frisbee
main:
-858993460
1 Frisbee
As you can see, from the main, its outputting the wrong thing. Instead of outputting the id followed by two spaces then the first name and last name, it outputs the number: -858993460. This has gotta be some sort of memory leak issue or something, but I'm pretty sure my copy constructor, overloaded assignment operator, and deconstructor are all defined correctly, but you can take a look at them.
I would appreciate any help at all as I am getting pretty desperate here. Thanks.
Your actual operator+ looks correct. But there are bugs in your copy-constructor and assignment-operator that would cause it to malfunction:
The copy-constructor does not set size, id, or the names.
The copy-constructor should allocate [size] items, not [itemsCheckedOut].
The assignment operator does not copy size.
The assignment operator allocates a new array whose dimension is the old size, probably causing an immediate buffer overflow.
The checkOut function does not check that it doesn't write beyond size. It needs to detect this case and either reject the checkout, or allocate more space. (I mentioned this last time you posted a question about this project)
It calls copy constructor:
Student(const Student &other) {
itemsCheckedOut = other.itemsCheckedOut;
array = new string[itemsCheckedOut];
for (int i = 0; i < itemsCheckedOut; i++) {
array[i] = other.array[i];
}
}
but you forget to copy all Student's fields in its body.
You override default copy constructor, so you should manually execute all data copying, as in assignment operator.
You should replace your string* array with a std::vector. It will handle the memory management for you, make your code far easier and less error prone than the manual memory management you are currently using. You can reserve an initial size of 10 if you are worried about it doing allocations when adding items (although with such small data sizes that shouldn't ever be a problem).
Related
I've created a class Student which contains a dynamic array. I've filled the first two items with the constructor. Every method I've tried to use to access/print those two elements from the main get a read/access violation and crashes. I've added a cout in the constructor that shows the elements ARE filled and exist. I've included two failed methods in the main: A void function that attempts to send first element to cout, and a method that accepts an int for the desired index. Both have been commented out to allow a test run showing elements are created and printed by the constructor.
Header:
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <iostream>
#define ARRAY_MAX 15
using namespace std;
class Student
{
private:
string firstName, lastName;
unsigned int ID, numItems = 0;
typedef string* StringPtr;
StringPtr items;
public:
int capacity = 15;
Student();
Student(const string fName, const string lName, const unsigned int id);
string getfName() const;
string getlName() const;
void getItem(int num);
string getItemB(int num) const;
unsigned int getID() const;
};
#endif // STUDENT_H
Definitions:
#include "student.h"
using namespace std;
Student::Student()
{
}
Student::Student(const string fName, const string lName, const unsigned int id)
{
firstName = fName;
lastName = lName;
ID = id;
StringPtr items = new string[capacity];
numItems = 0;
items[0] = "stuff";
items[1] = "things";
cout << items[0] << endl << items[1] << endl;
}
string Student::getfName() const
{
return firstName;
}
string Student::getlName() const
{
return lastName;
}
void Student::getItem(int num)
{
cout << items[0] << endl;
}
string Student::getItemB(int num) const
{
return items[num];
}
unsigned int Student::getID() const
{
return ID;
}
Main:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "student.h"
using namespace std;
int main()
{
Student stu;
string str;
stu = Student("John", "Smith", 1200);
cout << stu.getfName() << " " << stu.getlName() << endl;
//stu.getItem(0);
//cout << stu.getItemB(0);
system("pause");
// Quit without error
return 0;
}
Solution
Change
StringPtr items = new string[capacity];
into
items = new string[capacity];
TL;DR
In
Student::Student(const string fName, const string lName, const unsigned int id)
{
firstName = fName;
lastName = lName;
ID = id;
StringPtr items = new string[capacity];
numItems = 0;
items[0] = "stuff";
items[1] = "things";
cout << items[0] << endl << items[1] << endl;
}
The line
StringPtr items = new string[capacity];
creates a new Automatic (local) variable items and initializes it rather than the intended private member variable StringPtr items;. This is commonly known as Variable Shadowing. The local items goes out of scope at the end of the function leaking the allocated memory and the member items is never initialized leading to stu.getItem(0); accessing an uninitialized pointer and triggering the crash.
void Student::getItem(int num)
{
cout << items[0] << endl; // items points Crom knows where, so items[0] is invalid.
}
This crash is quite fortunate. Accessing an unitintialized pointer results in Undefined Behaviour which means it could do anything from looking like it works to causing the universe to explode.
The next problem you have to deal with is observing The Rule of Three.
The best way to deal with this is std::vector
std::vector<string> items;
std::vector is Rule of Three (Rule of Five, actually) compliant so that you don't have to be.
If std::vector is not allowed, you need to implement a copy constructor
Student::Student(const Student & src):
firstName(src.firstName), lastName(src.lastName),
ID(src.ID), numItems(src.numItems),
items(new string[capacity])
{
for (int i = 0; i < src.numItems; i++)
{
items[i] = src.items[i];
}
}
and an assignment operator (Taking advantage of the Copy and Swap Idiom for simplicity)
Student & Student::operator=(Student src)
{
swap(*this,src);
return *this;
}
Writing swap I'll leave up to you.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
The code I'm working with comes from Ivor Horton's "Beginning Visual C++ 2010".
Specifically Ex10_02.cpp using the Person.h.
The sort function does not work on my class objects. When names are entered and Enter key is finally pressed, the list of names is output in the order given in the input and after the sort() function is called, the names are again output to the screen. They should be sorted based on the second name. But they are output in the same order that I enter them initially.
I get no compile errors. I have tested the operator<() for my class and it works when comparing two objects of the class Person. But again the sort() function for the vector of Person objects is not correctly working. I don't have any more ideas on why this is happening. Any insight would be welcomed.
The code is as follows:
// Ex10_02
// storing objects in a vector
#include <iostream>
#include <vector>
#include <algorithm>
#include "Person.h"
using namespace std;
int main()
{
vector<Person> people; // Vector of Person objects
const size_t maxlength(50);
char firstname[maxlength];
char secondname[maxlength];
vector<int> numbers;
// input all the people
while(true)
{
cout << "Enter a first name or press Enter to end:";
cin.getline(firstname, maxlength, '\n');
if(strlen(firstname) == 0)
break;
cout << "Enter the second name: ";
cin.getline(secondname, maxlength, '\n');
people.push_back(Person(firstname, secondname));
}
// Output the contents of the vector
cout << endl;
auto iter(people.begin());
while(iter != people.end())
iter++->showPerson();
sort(people.begin(), people.end());
cout << endl;
for(auto i = 0; i < people.size(); i++)
{
people[i].showPerson();
}
cout << endl;
for(auto i = 0; i < people.size(); i++)
{
people[i].showPerson();
}
cout << endl;
cout << "Is people[0] < people[1] ?" << endl;
if(people[0] < people[1])
{
cout << "YES" << endl;
people[0].showPerson ();
people[1].showPerson();
}
else
{
cout << "NO" << endl;
people[1].showPerson();
people[0].showPerson();
}
cout << endl;
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
for(int i = 0; i < 8; i++)
cout << myvector[i] << endl;
cout << endl;
// using default comparison (operator <):
sort(myvector.begin(), myvector.end()); //(12 32 45 71)26 80 53 33
for(int i = 0; i < 8; i++)
cout << myvector.at(i) << endl;
return 0;
}
//-----------------------------------------------------------------------
Person.h:
// A class defining people by their names
//#pragma once
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <cstring>
#include <iostream>
using namespace std;
class Person
{
public:
// Constructor, includes no-arg constructor
Person(const char* first = "John", const char* second = "Doe")
{
initName(first, second);
}
// Copy constructor
Person(const Person& p)
{
initName(p.firstname, p.secondname);
}
// Destructor
~Person()
{
delete[] firstname;
delete[] secondname;
}
// Assignment operator
Person& operator =(const Person& p)
{
// deal with p = p assignment situation
if(&p == this)
{
return *this;
delete[] firstname;
delete[] secondname;
initName(p.firstname, p.secondname);
return *this;
}
}
// Less-than operator
bool operator <(const Person& p)
{
int result(strcmp(secondname, p.secondname));
if(result < 0 || result == 0 && strcmp(firstname, p.firstname) < 0)
return true;
return false;
}
// output a person
void showPerson() const
{
cout << firstname << " " << secondname << endl;
}
private:
char* firstname;
char* secondname;
// private helper function to avoid code duplication
void initName(const char* first, const char* second)
{
size_t length(strlen(first)+1);
firstname = new char[length];
strcpy_s(firstname, length, first);
length = strlen(second) + 1;
secondname = new char[length];
strcpy_s(secondname, length, second);
}
};
#endif // PERSON_H_INCLUDED
Change your assignment operator like this:
// Assignment operator
Person& operator =(const Person& p)
{
// deal with p = p assignment situation
if (&p != this)
{
delete[] firstname;
delete[] secondname;
initName(p.firstname, p.secondname);
}
return *this;
}
You have wrong &p == this check, return too early and no return path on the else path (at the end).
And your comparison operator like already pointed by #StoryTeller to:
if (result < 0 || ( result == 0 && strcmp(firstname, p.firstname) < 0 ))
You can also make your comparison operator const, use std::string etc.
I am in need of assistance.
I am trying to create a vector structure where the string contents inside of a char array get automatically assign to another STRING variable within the same structure.
After countless hours, I have not been able to figure it out. When I used function such as string(). It doesn't copy anything. I can't seem to assign char variable to string variable. Please advise.
#include <iostream>
#include <ctime>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int NAME_SIZE = 25;
struct tableInfo
{
char name2[NAME_SIZE];
string name = str(name2); // I need name string to equal name2 character variable
string info;
string link;
};
vector<tableInfo> table(6);
bool compareByWord(const tableInfo &lhs, const tableInfo &rhs);
int main()
{
cin.getline(table[0].name2, 51);
cin.getline(table[1].name2, 51);
cin.getline(table[2].name2, 51);
cin.getline(table[3].name2, 51);
cin.getline(table[4].name2, 51);
sort(table.begin(), table.end(), compareByWord);
cout << table[0].name << endl;
cout << table[1].name << endl;
cout << table[2].name << endl;
cout << table[3].name << endl;
cout << table[4].name << endl;
}
bool compareByWord(const tableInfo &lhs, const tableInfo &rhs)
{
unsigned int length = lhs.name.length();
if (rhs.name.length() < length)
length = rhs.name.length();
int sameLetters = 0;
for (unsigned int i = 0; i < length; ++i)
{
if (sameLetters == length)
return false;
if (tolower(lhs.name[i]) == tolower(rhs.name[i]))
{
++sameLetters;
continue;
}
return(lhs.name[i] < rhs.name[i]);
return(lhs.name[i] < rhs.name[i]);
}
return false;
}
If you want something to happen in a structure (or class), consider making sure it happens by using the constructor. You will need to change your vector so you can no longer default construct the struture, but that's ok.
This works, by enforcing the two fields contain identical data (not withstanding 25 no being long enough):
const int NAME_SIZE = 25;
struct tableInfo
{
tableInfo(char * whatever); //Make it so
char name2[NAME_SIZE];
string name;
string info;
string link;
};
tableInfo::tableInfo(char *whatever)
: name(whatever)
{
strncpy(name2, whatever, NAME_SIZE); //Now they match - unless whatever was too big
}
//vector<tableInfo> table(6);//no longer ok with the non-default constructor
bool compareByWord(const tableInfo &lhs, const tableInfo &rhs);
int main()
{
vector<tableInfo> table;
for (int i = 0; i < 5; ++i)
{
char name2[NAME_SIZE];
cin.getline(name2, 51); //Do you really mean 51?
// It's smaller than NAME_SIZE
table.emplace_back(name2);
}
sort(table.begin(), table.end(), compareByWord);
cout << table[0].name << endl;
cout << table[1].name << endl;
cout << table[2].name << endl;
cout << table[3].name << endl;
cout << table[4].name << endl;
}
How about doing like this:
struct tableInfo
{
char name2[NAME_SIZE];
string name;
string info;
string link;
tableInfo(const char* in_name)
{
memset(name2, 0, NAME_SIZE);
strncpy(name2, in_name, NAME_SIZE - 1);
name = string(name2);
}
};
How to use:
char tmp[NAME_SIZE];
cin.getline(tmp, 51);
table[0] = tableInfo(tmp);
I argue that you don't even need an array in the struct. Just use std::string. getline supports reading into a std::string and you can access individual characters using str[]. If you need to get a pointer to the data you can use str.c_str().
If you really need to use an array in the struct you can create a member function to return a std::string:
struct tableName {
char name[NAME_SIZE];
std::string info;
std::string link;
std::string nameString() const { return name; }
};
Parsing a file and need to add students to a struct vector using an array for student names specific to that course line.
In my course.h file:
struct Course {
std::string name;
int enrollment;
int maxEnrollment;
std::string* students; ///< array of student names
Course(std::string courseName, int maxEnrollmentPermitted);
bool enroll(std::string studentName);
void print(std::ostream& output);
};
In my course.cpp file:
bool Course::enroll(std::string studentName) {
this->students = new std::string[studentName];
if (this->enrollment < this->maxEnrollment) {
this->enrollment++;
return true;
}
else {
return false;
In my source file:
void processEnrollmentRequests(istream& enrollmentRequestsFile, vector<Course>& courses) {
// Read the requests, one at a time, serving each one
string courseName;
enrollmentRequestsFile >> courseName;
while (enrollmentRequestsFile) {
enrollmentRequestsFile >> ws;
string studentName;
getline(enrollmentRequestsFile, studentName);
int pos = search(courses, courseName);
if (pos >= 0) {
// Found a matching course
bool enrolled = courses[pos].enroll(studentName);
if (enrolled) {
cout << studentName << " has enrolled in " << courseName << endl;
}
else {
// course is full
cout << studentName << " cannot enroll in " << courseName << endl;
}
}
else {
// course does not exist
cout << studentName << " cannot enroll in " << courseName << endl;
}
enrollmentRequestsFile >> courseName;
}
}
}
}
I cant seem to add the gathered studentName to the array using this->students = new std::string[studentName]. Getting an error that says must have integral or enumeration type.
new SomeThing[size] is used to declare array. It makes no sense to use a string as the size.
Assuming the size of students is limited to maxEnrollment, you can use this:
if (this->enrollment < this->maxEnrollment) {
this->students[this->enrollment++] = studentName;
return true;
}
else {
return false;
For the sake of completeness, the allocation of students is not the only problem. Given that the code you posted also uses std::vector<Course>, and Course does not follow the rule of 3, using it in a std::vector is highly likely to cause memory corruption, leaks, etc.
Given that you state that students must remain a pointer, the complete fix is to write Course in this manner:
#include <string>
#include <algorithm>
struct Course {
std::string name;
int enrollment;
int maxEnrollment;
std::string* students; ///< array of student names
Course(std::string courseName, int maxEnrollmentPermitted);
bool enroll(std::string studentName);
void print(std::ostream& output);
Course(const Course& rhs);
Course& operator =(const Course& rhs);
~Course();
};
Course::Course(const Course& rhs) : name(rhs.name),
enrollment(rhs.enrollment),
maxEnrollment(rhs.maxEnrollment),
students(new std::string[rhs.maxEnrollment])
{
for (int i = 0; i < maxEnrollment; ++i)
students[i] = rhs.students[i];
}
Course& Course::operator= (const Course& rhs)
{
Course temp(rhs);
std::swap(temp.students, students);
std::swap(temp.maxEnrollment, maxEnrollment);
std::swap(temp.enrollment, enrollment);
std::swap(temp.name, name);
return *this;
}
Course::~Course() { delete [] students; }
Course::Course(std::string courseName, int maxEnrollmentPermitted) :
name(courseName),
enrollment(0),
maxEnrollment(maxEnrollmentPermitted),
students(new std::string[maxEnrollmentPermitted])
{}
Why all of this code? Well, in the code you posted in your question, you are using a std::vector<Course>. The Course class as written could not be safely used in a vector, due to Course having incorrect copy semantics. Thus your error you're getting may have a lot to do with code you stated wasn't yours (the vector<Course>).
The adjustments to Course above now makes Course safe to be used in a vector since the copy semantics (copy constructor, assignment operator, and destructor) have now been implemented to handle the dynamically allocated students member.
Note that absolutely none of this code would be necessary if students were simply a std::vector<std::string> instead of std::string *.
For more reading:
What is the rule of 3?
What is the copy / swap idiom?
I am trying to create a program that uses class, arrays, and functions to show information about two students(Name, id#, classes registered). The part I am struggling with is passing arrays to a function. How do I do that?
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class Student // Student class declaration.
{
private:
string name;
int id;
string classes;
int arraySize;
public:
void setName(string n)
{
name = n;
}
void setId(int i)
{
id = i;
}
void setClasses(string c, int num)
{
classes = c;
arraySize = num;
}
string getName()
{
return name;
}
int getId()
{
return id;
}
void getClasses()
{
for (int counter=0; counter <arraySize; counter++) {
cout << classes[counter] << endl;
}
}
};
int main()
{
//Student 1
string s1Name = "John Doe";
int s1Id = 51090210;
int const NUMCLASSES1 = 3;
string s1Classes[NUMCLASSES1] = {"C++","Intro to Theatre","Stagecraft"};
//Student 2
string s2Name = "Rick Harambe Sanchez";
int s2Id = 666123420;
int const NUMCLASSES2 = 2;
string s2Classes[NUMCLASSES2] = {"Intro to Rocket Science","Intermediate Acting"};
//
Student info;
info.setName(s1Name);
info.setId(s1Id);
//info.setClasses(s1Classes, NUMCLASSES1);
cout << "Here is Student #1's information:\n";
cout << "Name: " << info.getName() << endl;
cout << "ID: " << info.getId() << endl;
//cout << "Classes: " << info.getClasses() << endl;
info.setName(s2Name);
info.setId(s2Id);
// info.setClasses(s2Classes, NUMCLASSES1);
cout << "\n\nHere is student #2's information:\n";
cout << "Name: " << info.getName() << endl;
cout << "ID: " << info.getId() << endl;
//cout << "Classes: " << info.getClasses() << endl;
return 0;
}
The usual way to pass around variable-length lists in C++ is to use an std::vector. A vector is a single object that you can easily pass to a function, copying (or referencing) its contents. If you are familiar with Java, it's basically an ArrayList. Here is an example:
#include <vector>
#include <string>
using namespace std;
class foo {
private:
vector<string> myStrings;
public:
void setMyStrings(vector<string> vec) {
myStrings = vec;
}
}
//...
foo myObj;
vector<string> list = {"foo","bar","baz"};
myObj.setMyStrings(list);
If don't want to use the standard library though, you can pass an array C-style. This involves passing a pointer to the first element of the array, and the length of the array. Example:
void processStrings(string* arr, int len) {
for (int i = 0; i < len; i++) {
string str = arr[i];
//...
}
}
string array[] = {"foo","bar","baz"};
processStrings(array, 3); // you could also replace 3 with sizeof(array)
Passing raw arrays like this, especially if you wanted to then copy the array into an object, can be painful. Raw arrays in C & C++ are just pointers to the first element of the list. Unlike in languages like Java and JavaScript, they don't keep track of their length, and you can't just assign one array to another. An std::vector encapsulates the concept of a "list of things" and is generally more intuitive to use for that purpose.
Life lesson: use std::vector.
EDIT: See #nathanesau's answer for an example of using constructors to initialize objects more cleanly. (But don't copy-paste, write it up yourself! You'll learn a lot faster that way.)
You can pass array of any_data_type to function like this
void foo(data_type arr[]);
foo(arr); // If you just want to use the value of array
foo(&arr); // If you want to alter the value of array.
Use std::vector. Also, don't add functions you don't need. Here's an example of using std::vector
#include <string>
#include <iostream>
#include <vector>
using std::string;
using std::vector;
class Student // Student class declaration.
{
private:
vector<string> classes;
string name;
int id;
public:
Student (const vector<string> &classesUse, string nameUse, int idUse) :
classes (classesUse),
name (nameUse),
id (idUse)
{
}
void print ()
{
std::cout << "Name: " << name << std::endl;
std::cout << "Id: " << id << std::endl;
std::cout << "Classes: ";
for (int i = 0; i < classes.size (); i++)
{
if (i < classes.size () - 1)
{
std::cout << classes[i] << ", ";
}
else
{
std::cout << classes[i] << std::endl;
}
}
std::cout << std::endl;
}
};
int main()
{
Student John ({"C++","Intro to Theatre","Stagecraft"},
"John",
51090210);
John.print ();
Student Rick ({"Intro to Rocket Science","Intermediate Acting"},
"Rick",
666123420);
Rick.print ();
return 0;
}
Name: John
Id: 51090210
Classes: C++, Intro to Theatre, Stagecraft
Name: Rick
Id: 666123420
Classes: Intro to Rocket Science, Intermediate Acting
In the private variables of class Student, you are storing a string:
String classes;
where as you should be storing an array of strings like:
String classes[MAX_NUM_CLASSES];
then in the set classes function, pass in an array of strings as the first argument, so it should be :
void setClasses(string[] c, int num)
{
classes = c; //not sure if simply setting them equal will work, rather copy entire array using a for loop
arraySize = num;
}
This should point you in the right direction
Also, use std::vector instead of string[], it will be easier.