Please Help !

The C++ syntax has real molested me. I am completely stuck. For those who has assisted me before and the other programmers in the forum, i have this problem.

I wanted a syntax that will create a textfile database. I would like the database to have about 15 columns/ fields with headings that i have included in the struct below. The rows will be determined by the user and thus for every record user will input information against the column/ field heading for the column. Therefore the information is entered in a table with the following headings for the columns:
LR_UNIT_NUMBER | NAME_OF_PROPRIETOR | NATIONAL_ID_CARDNUMBER |


I would want to use a 2D vector to:

(a) populate and maintain the database at runtime by taking user input and write to the textfile database. //kindly assist how to "cout" and write with fstream object to the textfile database

(b) Read the whole database when a user prompts to read and display the contents of the database.

(c) Receive a string from user to query (by comparing with database) and Find a specific data from the database and return whether the record has been found within the database and the number of times such a record exists in the database.


//I created this small code from what i understand i should do but it cant do (I have not written the function to write to textfile since this initial code failed )

#include <vector>
#include <iostream>
#include <string>

using namespace std;

struct node
{
//I would like these to be the column headings which should althrough occupy the first row of the database/ vector.

std::string LR_UNIT_NUMBER;
std::string NAME_OF_PROPRIETOR;
int NATIONAL_ID_CARDNUMBER;
std::string ADDRESS_OF_PROPRIETOR;
char PIN_NUMBER_OF_PROPRIETOR;
char DATE_OF_ACQUISITION;
std::string DATE_OF_ISSUE_OF_TITLEDEED;
std::string CATEGORY;
string TYPE_OF_OWNERSHIP;
char COUNTY_LOCATED;
char DISTRICT_LOCATED;
char DIVISION_LOCATED;
char LOCATION_LOCATED;
char SUBLOCATION_LOCATED;
char VILLAGE_LOCATED;


};

int main()
{
int counter;
cout<<"How many land records do you want to create in one instance ?";
cin>>counter;


struct node use; // created a struct object

vector < vector <node> >vett1; //including the struct to declare the data types

if (counter ==1)

for (int i=0; i<vett1.size (); i++) //looping through the the columns of vector - though i highly doubt it here

{for (int j =0; j<15; j++;) //to loop through the row for each of the columns

{


vett1 [i][j].push_back(node ()); // receive the user input

cout<<[i][j]; //display input entered to screen


}

}
Last edited on
I know this is completely irrelevant on my part, but if you want a database, why use C++, when you could use things like SQL.
First please use code tags.

Second why are you mixing std::string with C-strings in your structure? I recommend you stick with the std::string.

You may want to study this tutorial on file input and output:
http://www.cplusplus.com/doc/tutorial/files/
I have learnt alot from this forum.

Someone showed to declare the vector using typedef as follows and would be glad to know how to loop through the columns and rows given such a declaration:



typedef std::string field;
typedef std::vector<field> record;
typedef std::vector<record> table;

Sorry i was away for awhile. I had not seen those responses. I didnt mention that i am preparing a college project and thus i require a simple database which am informed can be prepared using C++ syntax. have never employed SQL syntax.

Is there any way of initializing a 2D vector of struct using user input, then writing its input to textfile which can then be read from by an fstream object ?
Last edited on
Jib, ok i will maintain the variables as strings.

Who knows the syntax for 2D vector of structs ? And what about its implementation ? ? And how to push_back into that vextor ? If not possible, can i use the getline () ?
Last edited on
What is your attempt here?
Your struct should have a constructor because it contains chars and ints that won't get well-defined values otherwise.

Now here's the big question: do you need the ability to modify existing records in the database? If so then a simple text file will be difficult because you'll have to write all the subsequent records too. In other words, if the file has 100 records and you want to modify record 15 then you'll have to rewrite records 15-100. This is because the space occupied by record 15 will probably grow or shrink if you change it. Now it looks like you modified your original post so if all those strings used to be fixed-length character arrays then I'd change them back. If you use fixed length strings then you will know the size of each record and you can modify any record without affected the others.

Of course this changes the file slightly: it makes it a binary file that stores text data.
1
2
typedef std::string field;
typedef std::vector<field> record;

I disagree with this. Think about it: if this was a good idea then we wouldn't have named members in classes at all. Everything would just be of type record.
Thanks Dhyaden. I am waiting for a response. I would like the text file contents not to be overwritten. There is no problem if the variables become fixed, therefore the strings datatype is okay.

That agreed, how will i push_back the struct into the 2D vector and thus populate the 2D vector with user input at runtime ? Then write contents to file ? Then the function to read whole file and display to screen ? How about checking for a particular string or strings from the textfile and compare with a user input string and display "Owner name " and "land number" has been found in the file and exists "number" times ?

I would want a syntax with no lambdas so i may easily understand so i may play around to put more functions.

Thanks in advance.

Last edited on
One of the goals of a database is to not read all the records into memory. So I'd see if you can avoid reading everything into a 2D vector.

To start, go through the operations that you've mentioned and try to write function prototypes for each one. You may have to do this a couple of times to get it right. Once you have the prototypes, start filling in the code. I'll give you some examples from your last post:

how will i push_back the struct into the 2D vector and thus populate the 2D vector with user input at runtime ?

I think you don't want to put it into a 2D vector, but you will want to prompt the user for a record, so make a method for struct node:
1
2
// prompt the user for each field and populate the record with the results
bool getFromUser();


Then write contents to file ?
1
2
// write the record to the stream in binary format
bool write(ostream &);


Then the function to read whole file and display to screen ?

// read the record from the stream
bool read(istream &);
1
2
// Display the record in human-readable form
bool display();

How about checking for a particular string or strings from the textfile and compare with a user input string

You will need code that reads all records from a file. Start by writing this code and displaying each record. Once that works, change the code so it only prints the matching records.
Topic archived. No new replies allowed.