C++ ostream output wrong [closed] - c++

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 7 years ago.
Improve this question
I have a problem and I can't solve it.
note: I can't use any string/strlen/strcmp and such functions
My problem is when I'm trying to print obj after cin, when I don't put him value it prints garbage, and if I put value it change it by cin it print it well.
#ifndef __STRING_H__
#define __STRING_H__
#include <iostream>
using namespace std;
class String {
private:
char* Str; //pointer to string
int Str_s; // size of string public:
String(char* str = NULL);
~String();
void const print() const; //check
//~~~~~~~~~~~~~~~~~~
// operators
//~~~~~~~~~~~~~~~~~
String& operator=(const String& str);
bool operator==(const String& s) const;
bool operator!=(const String& s) const;
void operator-=(const char c);
void operator+=(const char c);
char& operator[](const int index) const;
//~~~~~~~~~~~~~~~~~~
// i/o ~~ operators
//~~~~~~~~~~~~~~~~~
friend ostream& operator<<(ostream&, const String& s);
friend istream& operator>>(istream&, String& s);
//~~~~~~~~~~~~~~~~~~
// arrange method
//~~~~~~~~~~~~~~~~~
void Letters(); // arrange all letter in sentence
void Space(); //remove all unwanted spaces
void Set_Str_s(int num) {
this->Str_s = num;
}
int Get_Str_s() {
return this->Str_s;
}
//~~~~~~~~~~~~~~~~~~
// methods
//~~~~~~~~~~~~~~~~~
char const* STR() {
return Str;
}
};
#endif
ostream& operator<<(ostream& output, const String& s) {
//char *c = s.Str;
//output << c << endl;
for (int i = 0; i < s.Str_s; i++) {
cout << s.Str[i];
}
return output;
}
istream& operator>>(istream& input, String& s) {
if (s.Str == NULL) {
s.Str_s = 0;
char temp[BUFFER];
int i = 0, j = 0;
cin.getline(temp, BUFFER);
while (temp[i] != NULL) {
s.Str_s++;
i++;
}
//s.Str = new char[s.Str_s];
s.Str = temp;
} else {
input >> s.Str;
}
return input;
}
#include <iostream>
using namespace std;
#include "string_head.h"
#include "Definition_head.h"
#include "Menu_head.h"
int main() {
char* c = "hi";
char* h = "lilo";
String s, m(c);
cin >> s;
cin >> m;
cout << s;
cout << endl;
cout << m;
}

The following line is a problem:
s.Str = temp;
you are storing a pointer to a local array that will be deleted when the function returns. Use something like:
s.Str = strdup(temp);
If your platform doesn't support strdup, you can implement it. It's not too hard.
The following line is also a problem:
input >> s.Str;
If there isn't enough memory to hold the string being read, you will be writing into memory that you are not supposed to.

Related

I'm freeing memory twice - C++

I have done this program where I check if my 'date' class is correct. The problem is that when I run my test program, it returned my the following error:
Error in `./bin/test': double free or corruption (fasttop): 0x00000000019c07c0 *
The job of this class is read and store a 'date'(a year) and a few events (allocated in a string array). For example, a object of this class would be: 1998 EVENT1 EVENT2 EVENT3.
Operator >> reads the next format:1908#Fantasmagorie#The Taming of the Shrew#The Thieving Hand#The Assassination of the Duke of Guise#A Visit to the Seaside
Well, my problem is that I'm deleting some pointer twice or freeing some memmory twice, I have tried a lot of things but I don't know how to fix it (as you can see on my code, I have already tried to set all pointers to 0 when I delete them.):
Date class .h
#ifndef _date_HISTORICA_
#define _date_HISTORICA_
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
class date{
private:
int year;
int eventsNum;
int reserved;
string * str;
void resize(int r);
public:
date();
//date(int a, string *s, int n);
date(const date& d);
~date();
int getAge();
void addEvent(string& s);
friend ostream& operator<<(ostream& os, const date& d);
friend istream& operator>>(istream& is, date& d);
};
#endif
Date Class Code:
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<date.h>
using namespace std;
void date::resize(int r)
{
assert(r>=0);
if(r!=this->reserved)
{
if(r!=0)
{
string * aux = new string[r];
if(this->reserved>0)
{
int min=this->reserved<r?this->reserved:r;
for(int i=0; i<min; i++)
aux[i]=this->str[i];
delete[] this->str;
this->str=NULL;
}
this->str=aux;
this->reserved=r;
if(this->reserved<this->eventsNum)
this->eventsNum=this->reserved;
} else
{
if(this->reserved>0)
{
delete[] this->str;
this->str=NULL;
}
this->year=0;
this->eventsNum=0;
this->reserved=0;
}
}
}
date::date() : year(0), eventsNum(0), reserved(0), str(0){}
date::date(const date& d)
{
this->year=d.year;
this->eventsNum=d.eventsNum;
this->reserved=d.reserved;
this->str=new string[this->reserved];
for(int i=0; i<this->eventsNum; i++)
this->str[i]=d.str[i];
}
date::~date()
{
this->year=0;
this->eventsNum=0;
this->reserved=0;
if(this->str)
delete[] this->str;
this->str=NULL;
}
int date::getAge(){return this->year;}
ostream& operator<<(ostream& os, const date& d)
{
os << d.year;
for(int i=0; i<d.eventsNum; i++)
os << '#' << d.str[i];
os << endl;
return os;
}
void date::addEvent(string& s){
if (this->eventsNum == this->reserved){
if (this->eventsNum==0)
resize(1);
else
resize(2*this->reserved);
}
this->str[eventsNum]=s;
eventsNum++;
}
istream& operator>>(istream& is, date& d)
{
string line; char c;
is >> d.year >> c;
getline(is, line);
int n=1;
for(int i=0; i<line.length(); i++)
if(line[i]=='#')
n++;
d.eventsNum=n;
d.reserved=d.eventsNum;
delete[] d.str;
d.str=NULL;
d.str=new string[n];
stringstream ss(line);
for(int i=0; i<n; i++)
getline(ss, d.str[i], '#');
return is;
}
Test Program Class:
#include<iostream>
#include<fstream>
#include<cronologia.h>
#include<date.h>
using namespace std;
int main(int argc, char * argv[]){
cout << "STATE: IN PROGRESS" << endl;
cout << "TEST: (2)" << endl;
date d;
ifstream f("./data/name.txt");
while(f >> d)
{
cout << d;
}
date d1;
cin >> d1;
d=d1;
cout << d << endl;
}
Example file (wich should be read by date clas):
1900#Sherlock Holmes Baffled#The Enchanted Drawing
1901#Star Theatre#Scrooge, or, Marley's Ghost
1902#A Trip to the Moon
1903#The Great Train Robbery#Life of an American Fireman
1904#The Impossible Voyage
1905#Adventures of Sherlock Holmes; or, Held for Ransom
1906#The Story of the Kelly Gang#Humorous Phases of Funny Faces#Dream of a Rarebit Fiend
1907#Ben Hur#L'Enfant prodigue
1908#Fantasmagorie#The Taming of the Shrew#The Thieving Hand#The Assassination of the Duke of Guise#A Visit to the Seaside
Im so sorry for my English!!! :,(
Since there is no assignment overloading in your code, in the line
d=d1;
All the members of d1 will be copied to a new object d by value. Hence there will be two copies of the object date which have the same reference value in their member str. Those two will eventually get out of scope and both will be destructed. The first one will free the allocated memory while the other will try to free that same reference and that is why you get the error.
You need a copy assignment operator:
void swap(date& other)
{
using std::swap;
swap(year, other.year);
swap(eventsNum, other.eventsNum);
swap(reserved, other.reserved);
swap(str, other.str);
}
date::date(const date& d) : year(other.year), eventsNum(other.eventsNum), reserved(other.reserved), str(new string[other.reserved])
{
for(int i = 0; i < this->eventsNum; i++)
this->str[i] = d.str[i];
}
date& date::operator = (const date& d)
{
swap(*this, d);
return *this;
}
Might also be nice to provide a move constructor..

C++ flush() not working? Can't use endl

For a class assignment I have to overload the insertion and extraction operators. I'm having trouble getting it to print to the console.
EDITED
Sorry, this is my first time posting. I realize that I didn't post enough info for you guys, I have updated with what should be the necessary code
driver.cpp
#include "mystring.h"
#include <iostream>
using namespace std;
int main(){
char c[6] = {'H', 'E', 'L', 'L', 'O'}
MyString m(c);
cout << m;
return 0;
}
mystring.h
class MyString
{
friend ostream& operator<<(ostream&, const MyString&);
public:
MyString(const char*);
~MyString(const MyString&)
private:
char * str; //pointer to dynamic array of characters
int length; //Size of the string
};
mystring.cpp
#include "mystring.h"
#include <iostream>
#include <cstring>
using namespace std;
MyString::MyString(const char* passedIn){
length = strlen(passedIn)-1;
str = new char[length+1];
strcpy(str, passedIn);
}
MyString::~MyString(){
if(str != NULL){
delete [] str;
}
}
ostream& operator << (ostream& o, const MyString& m){
for(int i = 0; i < strlen(m.str); i++){
o << m.str[i];
}
o.flush();
return o;
}
use the ostream::flush() method. As in:
ostream& operator << (ostream& o, const MyString& m){
for(int i = 0; i < strlen(m.str)-1; i++){
o << m.str[i];
}
o.flush();
return o;
}
Don't try to flush from inside an inserter. None of the standard inserters does that. Just add std::cout << '\n'; after the call to your inserter in main.
The issue here is that std::cout is line buffered. That means it saves the inserted characters in an internal buffer until it sees a newline character (or until it's explicitly flushed). You'll see the same behavior if you insert a std::string object but don't end the line.

Overloading operator =, error in reading string

I am trying to write an overload to the = operator so that it allows you directly assign one student object to another student object. So it will copy all the private data members. Here is what I have so far.
.h
#ifndef PROJECT3HEADER_H
#define PROJECT3HEADER_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student();
void Setlname(string lname);
void Setfname(string fname);
void SetAverage(float Average);
void SetLettergrade(char lettergrade);
void SetTestScore1(float score1);
void SetTestScore2(float score2);
void SetTestScore3(float score3);
void SetTestScore4(float score4);
void SetTestScore5(float score5);
string Getlname()const;
string Getfname()const;
float GetAverage()const;
char GetLetterGrade()const;
float GetScore1()const;
float GetScore2()const;
float GetScore3()const;
float GetScore4()const;
float GetScore5()const;
//void operator = (const Student & rhs);
Student operator=(const Student &rhs);
private:
string lname,fname;
float testScore[5];
float Average;
char lettergrade;
};
ostream & operator << (ostream &, const Student & pt);
//istream& operator >> (istream& in, Student& pt);
#endif
Studentmem.cpp
#include "Project3Header.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student()
{
lname="";
fname="";
Average=0;
lettergrade=' ';
testScore[0]=0,testScore[1]=0,testScore[2]=0,testScore[3]=0,testScore[4]=0;
}
void Student::Setlname(string lname1){
lname=lname1;
}
void Student::Setfname(string fname1){
fname=fname1;
}
void Student::SetAverage(float average1){
Average=average1;
}
void Student::SetLettergrade(char lettergrade1){
lettergrade=lettergrade1;
}
void Student::SetTestScore1(float score1){
testScore[0]=score1;
}
void Student::SetTestScore2(float score2){
testScore[1]=score2;
}
void Student::SetTestScore3(float score3){
testScore[2]=score3;
}
void Student::SetTestScore4(float score4){
testScore[3]=score4;
}
void Student::SetTestScore5(float score5){
testScore[4]=score5;
}
string Student::Getlname()const {
return lname;
}
string Student::Getfname()const {
return fname;
}
float Student::GetAverage() const{
return Average;
}
char Student::GetLetterGrade()const{
return lettergrade;
}
float Student::GetScore1() const{
return testScore[0];
}
float Student::GetScore2() const{
return testScore[1];
}
float Student::GetScore3() const{
return testScore[2];
}
float Student::GetScore4() const{
return testScore[3];
}
float Student::GetScore5() const{
return testScore[4];
}
Student Student::operator=(const Student &rhs){
lname = rhs.lname;
fname = rhs.fname;
for (int i = 0; i < 5; i++){
testScore[i] = rhs.testScore[i];
}
return *this;
}
std::ostream& operator<<(std::ostream& out, Student const& obj)
{
out << "Lname: " << obj.Getlname() << "\n";
out << "fname: " << obj.Getfname() << "\n";
out << "Average: " << obj.GetAverage() << "\n";
out << "Grade: " << obj.GetLetterGrade() << "\n";
return out;
}
But when I try to use it in the main I get an error... error reading characters of string.
here is a little of my main
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Project3Header.h"
using namespace std;
//Tyler Smith
void StdInfo(Student array[], int size);
Student * MakeStudentArray(int size);
int main(){
ifstream inData;
int size = 0;
int highsize = 0;
char data[65535];
inData.open("F:\\grade.dat");
if (!inData)
{
cout << "Error opening file.\n";
cout << "Perhaps the file is not where indicated.\n";
return 1;
}
while (inData.getline(data, 65535)) {
size++;
}
inData.close();
cout << size;
Student s1;
Student * ptArr;
ptArr = MakeStudentArray(size);
for (int i = 0; i < size; i++){
ptArr[i]= s1;
}
StdInfo(ptArr,size);
/* for (int i = 0; i < size; i++){
cout << ptArr[i].Getlname() << ptArr[i].Getfname(); //<< ptArr[i].Get() << endl;
}*/
cout << ptArr[2].Getfname();
return 0;
}
Student * MakeStudentArray(int size)
{
return new Student[size];
}
void StdInfo(Student array[], int size){
ifstream in;
in.open("F:\\grade.dat");
string fname1,lname1="";
int Score1, Score2, Score3, Score4, Score5=0;
for (int i = 0; i < size; i++) {
in >> lname1;
in >> fname1;
in >> Score1;
in >> Score2;
in >> Score3;
in >> Score4;
in >> Score5;
array[i].Setlname(lname1);
array[i].Setfname(fname1);
array[i].SetTestScore1(Score1);
array[i].SetTestScore2(Score2);
array[i].SetTestScore3(Score3);
array[i].SetTestScore4(Score4);
array[i].SetTestScore5(Score5);
array[i] = array[i + 1];
//for (int j = 0; j < 5; j++) {
//in >> array[i].testScore[j];
//}
}
}
When I try it simply will not work. When I do it in debug mode it stops at the overloading operator and stays "rhs error reading characters of string"
The essense of your problem is not the (unconventional) assignment operator, but the MakeStudentArray function.
I hardcoded the size to 10, rather than reading it from a file, and used
Student * MakeStudentArray(int size)
{
return new Student[size];
}
taking care to call delete [] ptArr; after I was done in main (worrying about exceptions as I did) and all was well.
Your version allocates Students (maybe all over the heap), rather than an array.
If I change the question code you posted in main to
for (int i = 0; i < size; i++){
ptArr[i].operator= (s1);
//^------- you said 0, right?
}
then I get a problem. This is because the for loop assumes the "array" is contiguous, but your function newed an array of points, not an array of Students, so it walks into goodness only knows where when it tries to loop over the "array".
Using my suggestion version should solve the problem, since it allocates an array using new[size] and also makes it easier to delete.
Edit 1:
With the rest on main now posted, perhaps you do keep trying to set ptArr[0] several times in the loop, but then calling StdInfowill try to walk an "array" and will give you the error. If you wish to index into an array, using [] it must be contiguous, so the allocation function, MakeStudentInfo will still be the cause of the problem.
Edit2:
Now there is even more code, look at your StdInfo function. Without the extra details it does this:
for (int i = 0; i < size; i++) {
//...
array[i] = array[i + 1];
}
What are you trying to achieve here? It looks like set the current array[i] to something we haven't got to yet. Once i gets to size i+1 will be off the end of your array.
Removing that line would be a good idea.
The assignment operator should return a reference to the assigned object, not a copy. Also think about using the copy & swap idiom
Student& Student::operator=(const Student &rhs){

overloading a member inside another member

in my school assignment i need a small help
this is my header file:
#include <iostream>
#include <cstring>
using namespace std;
#include "ISBNPrefix.h"
class ISBN
{
char str[11];
char area[6];
char publisher[8];
char title[7];
bool registered;
public:
ISBN();
ISBN(const char*,ISBNPrefix &);
void toStr(char*)const;
void toStrWithStyle(char*)const;
bool empty()const;
bool isRegistered() const;
bool read(istream& is, const ISBNPrefix& list);
void display(ostream&) const;
};
int isValid(const char* str);
and this is the implementation of my file:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <iomanip>
using namespace std;
#include "ISBN.h"
ISBN::ISBN()
{
str[0]='\0';
area[0]='\0';
publisher[0]='\0';
title[0]='\0';
registered=false;
}
ISBN::ISBN(const char* s,ISBNPrefix& p)
{
if(isValid(s)==1)
{
strcpy_s(str,s);
}
else
{
*this=ISBN();
}
}
bool ISBN::empty()const
{
bool chk=false;
if(str[0]=='\0')
chk=true;
return chk;
}
void ISBN::toStrWithStyle(char* s) const
{
if(registered)
{
sprintf(s,"%s-%s-%s-%c",area,publisher,title,str[9]);
}
else
{
toStr(s);
}
}
void ISBN::toStr(char* s) const
{
if (str[0]!='\0')
strcpy(s,str);
else
strcpy(s,"no data");
}
void ISBN::display(ostream & os) const
{
char str[14];
toStrWithStyle(str);
cout<< setw (13) <<str;
}
int isValid(const char* str)
{
int rc=0;
if(str!=0)
{
int sum,i=0;
sum=0;
for(i=0;i<10;i++)
sum+=(str[i]-'0')*(10-i);
if(sum%11==0)
{
rc= 1;
}
}
else
rc=0;
return rc;
}
bool ISBN::read(istream& is, const ISBNPrefix& list)
{
char str[11];
bool quit=false;
bool ok=false;
char lists;
do{
cout<<"ISBN (0 to quit) : ";
is.getline(str,11); //or is.get(str,11)
if(strcmp(str,"0")==0)
quit=true;
else if (isValid(str)==1)
{
*this=ISBN(str,list);
ok=true;
cout<<"isbn is valid"<<endl;
}
else
{
*this=ISBN();
cout<<"invalid ISBN"<<endl;
}
} while(!quit&&!ok);
return !quit;
}
in the ISBN::read where I say
*this=ISBN(str,list);
i want to overload another member but i can't.
can anyone tell me how can i do that?
First I would suggest use std::string in favour of char[]. It will save a lot of trouble. For reading ISBN I would write something like this:
bool ISBN::read(istream& is)
{
ISBN result;
// reading into result
std::swap(*this,result);
return !quit;
}
Or even better (as a non member function):
std::istream& operator>>(istream& is, ISBN& obj)
{
ISBN result;
// reading into result
is(!quit)
is.clear(std::ios_base::failbit);
std::swap(obj,result);
return is;
}
In any way you should RAII classes for your resources. In your special case std::string instead of char[].

Segmentation fault: 11 and malloc errors in C++ code

Ok, so I know there are probably a lot of errors in this code. I'm pretty new to dynamic memory allocation, pointers, etc.
The header file, account.h, is given to us by our professor. We were told not to make any changes to the .h file.
The implementation file is written by me. The main function is included just for basic initial testing. We were given another file to actually test the implementation of the account class.
If I don't comment out the cout name line, I get a seg fault 11 error.
If I do, it'll print the account number, but throw this error:
Test(29976) malloc: * error for object 0x62c1aa18c9d8374: pointer being freed was not allocated* set a breakpoint in malloc_error_break to debug
Abort trap: 6
Any help at all would be greatly appreciated!
Here's the header file:
class account
{
public:
typedef char* string;
static const size_t MAX_NAME_SIZE = 15;
// CONSTRUCTOR
account (char* i_name, size_t i_acnum, size_t i_hsize);
account (const account& ac);
// DESTRUCTOR
~account ( );
// MODIFICATION MEMBER FUNCTIONS
void set_name(char* new_name);
void set_account_number(size_t new_acnum);
void set_balance(double new_balance);
void add_history(char* new_history);
// CONSTANT MEMBER FUNCTIONS
char* get_name ( ) const;
size_t get_account_number ( ) const;
double get_balance( ) const;
size_t get_max_history_size( ) const;
size_t get_current_history_size ( ) const;
string* get_history( ) const;
friend ostream& operator <<(ostream& outs, const account& target);
private:
char name[MAX_NAME_SIZE+1]; //name of the account holder
size_t ac_number; //account number
double balance; //current account balance
string *history; //Array to store history of transactions
size_t history_size; //Maximum size of transaction history
size_t history_count; //Current size of transaction history
};
Here is the implementation file:
// File: account.cxx
// Author: Mike Travis
// Last Modified: Mar 3, 2012
// Description: implementation of Account class as prescribed by the file account.h
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "account.h"
using namespace std;
//Constructor
account::account(char* i_name, size_t i_acnum, size_t i_hsize){
string *d_history;
d_history = new string[i_hsize];
for(int i = 0; i<i_hsize; i++){
name[i] = i_name[i];
}
ac_number = i_acnum;
history_size = i_hsize;
history_count = 0;
}
account::account(const account& ac){
string *d_history;
d_history = new string[ac.history_size];
for( int i=0; i<ac.get_current_history_size(); i++){
strcpy(d_history[i], history[i]);
}
strcpy(name,ac.get_name());
ac_number = ac.get_account_number();
history_size = ac.get_max_history_size();
history_count = ac.get_current_history_size();
}
account::~account(){ delete [] history; }
void account::set_name(char* new_name){ strcpy(name, new_name); }
void account::set_account_number(size_t new_acnum){ ac_number = new_acnum; }
void account::set_balance(double new_balance){ balance = new_balance; }
void account::add_history(char* new_history){
strcpy(history[history_count], new_history);
history_count++;
}
char* account::get_name() const {
char* name_cpy;
strcpy(name_cpy, name);
return name_cpy;
}
size_t account::get_account_number() const{ return ac_number; }
double account::get_balance() const{ return balance; }
size_t account::get_max_history_size() const{ return history_size; }
size_t account::get_current_history_size() const{ return history_count; }
//string* account::get_history() const{ return *history; }
int main(){
account test1("mike travis", 12345, 20);
//cout<<"\nname: "<< test1.get_name();
cout<<"\n\nacnum: "<<test1.get_account_number()<<"\n\n";
return 0;
}
In the destructor of account, you delete the history array. However, in the constructor, you allocate (and leak) an array which is stored in the local variable d_history. You presumably wanted to assign that to the member variable history instead - since you haven't, if you get to the destructor it gives you an error saying that you're freeing history but have never allocated it.
There's a similar error in the copy constructor as well.
There are also other errors in your code as well, which I assume you'll find as you go - get_name(), for example, is not going to work. I suspect the header file is not helping here, but there's not much to be done if you're not supposed to change that.
I've written a little bit code for you and corrected the epic mistakes (even in the header file, sorry ;)). It is still extremely ugly and c-ish, but maybe you can learn something reading it:
#include <cstddef>
#include <ostream>
class account
{
// The whole class makes no sense, since it has no useful
// member function or anything like this.
// Furthermore, the class provides almost full access to all its member variables.
// At this point one could just make everything public.
// This is not even exception safe when the constructor throws.
// A good implementation would use history_entry and history classes,
// together with std::string and std::vector/std::deque
// And it would provide some sort of functionality. ;)
public:
account(const char* name, unsigned number, std::size_t history_max_size);
account(const account& other);
~account();
const char* name() const;
unsigned number() const;
double balance() const;
const char* const* history() const;
std::size_t history_size() const;
unsigned history_max_size() const;
void set_name(const char* new_name);
void set_number(unsigned new_number);
void set_balance(double new_balance);
void add_history(const char* new_history);
private:
char* name_;
unsigned number_;
double balance_;
char** history_;
std::size_t history_size_;
const std::size_t history_max_size_;
};
std::ostream& operator << (std::ostream& stream, const account& a);
#include <cassert>
#include <cstring>
account::account(const char* name, unsigned number, std::size_t history_max_size)
: name_(0)
, number_(number)
, balance_(0.0)
, history_(new char*[history_max_size])
, history_size_(0)
, history_max_size_(history_max_size)
{
assert(name != 0);
assert(history_max_size != 0);
set_name(name);
}
account::account(const account& other)
: name_(0)
, number_(other.number_)
, balance_(other.balance_)
, history_(new char*[other.history_max_size_])
, history_size_(other.history_size_)
, history_max_size_(other.history_max_size_)
{
set_name(other.name_);
for (std::size_t i = 0; i != other.history_size_; ++i)
{
history_[i] = new char[std::strlen(other.history_[i]) + 1];
strcpy(history_[i], other.history_[i]);
}
}
account::~account()
{
delete[] name_;
for (std::size_t i = 0; i != history_size_; ++i)
delete[] history_[i];
delete[] history_;
}
const char* account::name() const
{
return name_;
}
unsigned account::number() const
{
return number_;
}
double account::balance() const
{
return balance_;
}
const char* const* account::history() const
{
return history_;
}
std::size_t account::history_size() const
{
return history_size_;
}
unsigned account::history_max_size() const
{
return history_max_size_;
}
void account::set_name(const char* new_name)
{
if (name_)
delete[] name_;
name_ = new char[std::strlen(new_name) + 1];
std::strcpy(name_, new_name);
}
void account::set_number(unsigned new_number)
{
number_ = new_number;
}
void account::set_balance(double new_balance)
{
balance_ = new_balance;
}
void account::add_history(const char* new_history)
{
if (history_size_ == history_max_size_)
{
delete[] history_[0]; // delete oldest entry
for (std::size_t i = 0; i != history_size_ - 1; ++i)
history_[i] = history_[i + 1];
--history_size_;
}
history_[history_size_] = new char[strlen(new_history) + 1];
std::strcpy(history_[history_size_], new_history);
++history_size_;
}
std::ostream& operator << (std::ostream& stream, const account& a)
{
return stream << "account [name: " << a.name() << ", number: "
<< a.number() << ", balance: " << a.balance() << ']';
}
#include <iostream>
int main()
{
account a("Hello!", 500, 5);
a.set_balance(12.546);
for (int i = 50; i--; )
a.add_history("Yaaay..");
//account b = a;
std::cout << a << '\n';
}