MS ACCESS && C++

{NcsO}ReichstaG

[H]ard|Gawd
Joined
Aug 13, 2004
Messages
1,768
Hey.
I have a c++ assignment that requires something like a word bank. Essentially, the C++ program has to be able to enter this word bank and randomly pick a word out of about ~5000 words. I was considering using a MS Access database as the database for the words. However, I do not know how to connect my C++ program to the database. Can someone give me a link, or explain me how to do so.

-Thanks For Your Help & Time
 
I never made it past intro level C++, but wouldn't it just be easier to use a text file instead?
 
Well, you could go through writing the 2 pages of code to set up COM and use ADO to connect to the DB. Here's an example...

ADO with C++

Or you could just use a simple hashing algorithm to store the items in memory after reading them from a flat file. Hashing really isn't as bad as it sounds to implement.
 
It would be easier to use just a text file. However, I want to learn to use Databases with c++.
So are these my only two options:
"2 pages of code to set up COM and use ADO to connect to the DB"
"hashing algorithm to store the items in memory after reading them from a flat file"

No simpler way?

-Thanks for your help
 
Well, if you were looking to make something for real, I don't know if it take two pages really but the way is ADO. We all think you are making this harder than it has to be. But yes, generally to access a database you have to establish a connection, a recordset... then know when to operate against either... programming is knowing when to use what. You don't read in a bunch of numbers from a database.
 
if you want to use a database using c++ on windows. I suggest using the database provider's c/c++ api or using the .net framework.

Here's a quick example of the usage of .net objects/classes with standard c++ code.

Code:
using namespace System;
using namespace System::Data::OleDb;

using namespace std;

char* ConnectionString = "";

void AddWord(char* word);

char* GetRandomWord();

int main()
{
	//write your ansi c++ code
	cout << "1) enter word, 2) get a random word" << endl;
	while(true)
	{
		int choice;
		cin >> choice;
		switch(choice)
		{
		case 1:
			cout << endl << "enter the word to add (255 character limit)" << endl;
			char word[255];
			cin >> word;
			AddWord(word);
			break;
		case 2:
			cout << GetRandomWord() << endl;
			break;
		}
	}
	return 0;
}

//this function will convert a .net string to an ansi c string.
//.net strings are unicode and are stored on a heap in a different runtime, so the contents
//of the string need to be copied from the .net runtime to the c runtime's heap.
char* CopyToCString(System::String* str)
{
	IntPtr ptr = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str);
	try
	{
		char* source = (char*) ptr.ToPointer();
		char* dest = new char[strlen(source) + 1];
		strncpy(source, dest, strlen(source));
		return dest;
	}
	__finally
	{
		System::Runtime::InteropServices::Marshal::FreeHGlobal(ptr);
	}
}

//creates an oledb database connection to somewhere and executes 2 queries
char* GetRandomWord()
{
	OleDbCommand* cmd;
	OleDbConnection* conn = new OleDbConnection(ConnectionString);
	try
	{
		conn->Open();
		cmd = conn->CreateCommand();
		cmd->CommandText = S"SELECT COUNT(*) FROM [wordbank]";
		int id = Convert::ToInt32(cmd->ExecuteScalar());
		if(id == 0)
			return "EMPTY";
		cmd->CommandText = S"SELECT [word] FROM [wordbank] WHERE [word_id]=@id";
		cmd->Parameters->Add(S"@id", __box(id));
		System::String* str = cmd->ExecuteScalar()->ToString();
		return CopyToCString(str);
	}
	__finally
	{
		conn->Close();
		if(cmd != NULL)
			cmd->Dispose();
	}
}

//creates an oledb database connection to somewhere and adds a word
void AddWord(char* word)
{
	OleDbCommand* cmd;
	OleDbConnection* conn = new OleDbConnection(ConnectionString);
	try
	{
		conn->Open();
		cmd = conn->CreateCommand();
		cmd->CommandText = S"INSERT INTO [wordbank]([word]) VALUES(@word)";
		cmd->Parameters->Add(S"@word", new String(word));
		cmd->ExecuteNonQuery();
	}
	__finally
	{
		conn->Close();
		if(cmd != NULL)
			cmd->Dispose();
	}
}
 
If you're just searching a "word bank", just make an array of strings, then populate that array from a file or whatever you're given. If it's unsorted write and run a binary sort on them, then you'll be able to do a binary search to find any word in that word list.
 
#include<iostream>
#include<fstream>
#include<iomanip>
#include<conio.h>
#include <stdlib.h>

using std::ifstream;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::setiosflags;
using std::ios;


int main()
{

char string[256];
ifstream file;


file.open("Name.Of.File.txt");

if(!file)
{
cout << "Error: File could not be open.\n" << endl;
exit(1);
}


while(!file.eof())
{
file >> string;
}


cout << string << "\n" << endl;

cout << "Press any key to quit..." << endl;
getch();

return 0;
}


I was thinking code like that ^ that to fill an array with with the data from the file. ( I found that that code on the internet and have been expermenting with it.) However, I am also looking for the definitions of those words. In other words, I need to store ~5000 words and all of their definitions. Considering that, would this be impractical?
 
The above code looks fine for taking in a list of words from a file and putting them in an array of strings. just make sure the file doesn't have more than 256 items or you're going to be running into memory that isn't yours. You also need to index into your array, you can't just do file >> string. you need to do file >> string. I would recommend making a struct of two strings to hold a word and it's defintion, then you can just make an array of those structs. You'll be able to sort and search it just the same.

Oh, and make sure to use
Code:
 tags in the future please.
 
Back
Top