View Full Version: Basic text-based database

C++ Learning Community > C++ Works in Progress > Basic text-based database


Title: Basic text-based database
Description: difficulity implementing string arrays


REVELation - September 24, 2003 07:09 PM (GMT)
#include <iostream.h>
#include <fstream.h>

class Database
{
public:
Database (int);
~Database();

int GetIntArray(int) const;
void SetIntArray (int, int);
char GetCharArray(int) const;
void SetCharArray (int, char*);
int GetTotalEntries () const;
void SetTotalEntries (int);

private:
int * IntContents;
char * CharContents;
int TotalEntries;
};

Database :: Database (int Entries)
{
IntContents = new int[Entries];
CharContents = new char[Entries];
TotalEntries = Entries;
}

Database :: ~Database ()
{
delete [] IntContents;
IntContents = 0;
delete [] CharContents;
CharContents = 0;
}

int Database :: GetIntArray (int Entry) const
{
return *IntContents + Entry;
}

void Database :: SetIntArray (int Entry, int NewEntry)
{
int * ChangeIntEntry = IntContents + (Entry-1);
*ChangeIntEntry = *NewEntry;
}

char Database :: GetCharArray (int Entry) const
{
return *CharContents + Entry;
}

void Database :: SetCharArray (int Entry, char* NewEntry)
{
// char * ChangeCharEntry = CharContents[Entry-1];
// *ChangeCharEntry = *NewEntry;
CharContents [Entry] = new char [30];
*CharContents = *NewEntry;
}

int Database :: GetTotalEntries () const
{
return TotalEntries;
}

void Database :: SetTotalEntries (int Entries)
{
TotalEntries = Entries;
}

int main()
{
cout << "Would you like to:\n";
cout << "1) access an existing database?\n";
cout << "2) create a new database?\n";
int Choice;
cin >> Choice;

if (Choice == 1) //Database Access and Modification (AAM) Starts Here
{
cout << "Enter the filename of the database you wish to open (including extensions, eg. .txt, .dat):\t";
char Filename[15];
cin.getline (Filename, 15);
cin.getline (Filename, 15);
cin.getline (Filename, 15);
ifstream DataFile (Filename);
int EntriesNeeded;
DataFile >> EntriesNeeded;
Database * Current = new Database (EntriesNeeded);

while (1)
{
cout << "Would you like to:\n";
cout << "1) modify this database?\n";
cout << "2) view this database?\n";
cout << "3) exit the program?\n";
int Choice2;
cin >> Choice2;

switch (Choice2)
{
case 1: cout << "Which entry would you like change? (1-" << Current->GetTotalEntries() << ")\n";
int EntryToChange;
cin >> EntryToChange;

cout << "Enter a new label for this entry:\t";
char CharEntry[30];
cin.getline (CharEntry, 30);
Current->SetCharArray(EntryToChange, CharEntry);

cout << "Enter a new value for this entry:\t";
int IntEntry;
cin >> IntEntry;
Current->SetIntArray(EntryToChange, IntEntry);

cout << "Entry changed!\n";
break;

case 2: for (int x = 0; x < Current->GetTotalEntries(); x++)
{
cout << x << ". " << Current->GetCharArray(x) << "\t";
cout << Current->GetIntArray(x) << "\n";
}
cout << "\nDatabase displayed successfully!\n";
break;

case 3: cout << "Exiting program...\n";
return 0;

default: cout << "Invalid input, please enter either 1, 2 or 3.\n";
break;
}
}
} //Database AAM Ends Here

if (Choice == 2) //Database Creation Starts Here
{
cout << "How many entries would you like to create?\n";
int EntriesNeeded;
cin >> EntriesNeeded;

Database * Current = new Database (EntriesNeeded); //pointer used so current database loaded can be changed

for (int x = 1; x < Current->GetTotalEntries(); x++)
{
cout << "Enter a label for entry number " << x << " please: ";
char CharEntry[30];
cin.getline (CharEntry, 30);
cin.getline (CharEntry, 30);
Current->SetCharArray (x, CharEntry);

cout << "Enter a value for entry number " << x << " please: ";
int IntEntry;
cin >> IntEntry;
Current->SetIntArray (x, IntEntry);
cout << "\n";
}

cout << "\nDatabase creation complete!\n\n";

bool Repeat = true;
while (Repeat == true)
{
cout << "Would you like the database to be displayed now?\n";
char Choice3[4];
cin.getline (Choice3, 4);
cin.getline (Choice3, 4);

if (strcmpi (Choice3, "yes") == 0)
{
for (int x = 0; x < Current->GetTotalEntries(); x++)
{
cout << x+1 << ". " << Current->GetCharArray(x) << "\t";
cout << Current->GetIntArray(x) << "\n";
}
cout << "End of database.\n";
Repeat = false;
}

if (strcmpi (Choice3, "no") == 0)
{
Repeat = false;
}

if ((strcmpi (Choice3, "yes") != 0) && (strcmpi (Choice3, "no") != 0))
{
cout << "Invalid entry, please enter either yes or no.\n";
}
}

cout << "To what filename would you like to save your database? (must end with an extension, eg. .txt, .dat\n";
char Filename[15];
cin.getline (Filename, 15);
ofstream DataFile (Filename);

DataFile << Current->GetTotalEntries();
for (int x = 0; x < Current->GetTotalEntries(); x++)
{
DataFile << Current->GetCharArray(x) << "\t";
DataFile << Current->GetIntArray(x) << "\n";
}

//PERHAPS ENTER CODE TO CHECK IF DATA ENTERED INTO FILE CORRECTLY

cout << "Database saved to " << Filename << " successfully!\n\n";
cout << "Thank you for using DataCap, Created by Rob Percival (see readme.txt)\n\n";
cout << "Exiting Program...\n";
return 0;
} //Database Creation Ends Here

if (Choice != (1 || 2 || 3))
{
cout << "Invalid Input, Enter either 1 or 2!\n";
cout << "Exiting program...\n";
return 0;
}
}

-----------------------

made this ages ago, wen i was rely new 2 C++, not luked @ it recently but remember havin a problem with storing strings. no idea wat condition the program is in atm rely, ne1 who wants 2 try, ur welcome 2 fix it and post the code bak here.

Incubator - September 24, 2003 07:50 PM (GMT)
you could have simply included
#include <iomanip>
#include <sqlplus.hh>
and used their functions to build a DB class :)

REVELation - September 24, 2003 08:50 PM (GMT)
lol i cud hav dun that if i knew bout them and how 2 use em. thx, il hav a luk @ those libraries + try 2 figure out how 2 use em.




* Hosted for free by InvisionFree