I am using Turbo c++ 4.5, and everytime I run this program weird errors happen and sometimes it shuts down. Im going to replace the compiler but in the meantime I wanted to know if this program works for anyone else.
I know somethings look stupid but I was just messing around triying to fix it, maybe sometimes I dont free the memory or something like that.Code:#include <iostream.h> #include <string.h> #ifndef BUFFER_H #define BUFFER_H //class Buffer class Buffer { public: Buffer(); ~Buffer(); void Add(int Pos, char *b); void ClearBuff(); void DisplayBuff(); private: char *a; }; #endif Buffer::Buffer() { a = new char[1999]; } Buffer::~Buffer() { delete[] a; } void Buffer::Add(int Pos, char *b) { int i = 0; for (i = 0; i < strlen(b); i++) { if (Pos+i > 1999 || Pos+i < 0) return; a[Pos+i] = b[i]; } } void Buffer::ClearBuff() { int i = 0; for (i = 0; i < 1999; i++) a[i] = ' '; } void Buffer::DisplayBuff() { int i = 0; for (i = 0; i < 1999; i++) cout << a[i]; } /******************************************************************************/ #ifndef ITEM_H #define ITEM_H // class Item class Item { public: Item(); Item(char *a, float b); ~Item(); float GetPrice() { return Price; } char *GetName() { return Name; } void operator = (char *a); void operator = (float a) { Price = a; } private: float Price; char *Name; }; #endif Item::Item(): Price(0.0f), Name(NULL) { } Item::Item(char *a, float b): Price(b), Name(NULL) { int i = 0; if (Name != NULL) { if (strlen(Name) == 1) delete Name; else delete[] Name; } Name = new char[ strlen(a) + 1 ]; for (i = 0; i < strlen(a); i++) Name[i] = a[i]; } Item::~Item() { if (Name != NULL) { if (strlen(Name) == 1) delete Name; else delete[] Name; } } void Item::operator = (char *a) { int i = 0; if (Name != NULL) { if (strlen(Name) == 1) delete Name; else delete[] Name; } Name = new char[ strlen(a) + 1]; for (i = 0; i < strlen(a); i++) Name[i] = a[i]; } /******************************************************************************/ int xytobuff(int x, int y); void AddtoBuff(Buffer a, Item *b); int main() { Buffer a; Item b[5]; b[0] = "Zanahoria"; b[0] = 16.00; b[1] = "Papa"; b[1] = 19.90; b[2] = "Cebolla"; b[2] = 9.00; b[3] = "Pepino"; b[3] = 12.50; b[4] = "Papaya"; b[4] = 16.00; b[5] = "Sandia"; b[5] = 26.90; a.ClearBuff(); AddtoBuff(a, b); a.DisplayBuff(); return 0; } int xytobuff(int x, int y) { return x + y * 80; } void AddtoBuff(Buffer a, Item *b) { int x = 5, y = 1; int i = 0; while (i < 5) { a.Add(xytobuff(x, y), b[i].GetName()); y++; i++; } }



LinkBack URL
About LinkBacks


