473,287 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

C++ reading database from the file into the array

well i got this assignment which i dont even have a clue what i am
supposed to do. it is about reading me data from the file and load
them into a parallel array here is the question:

Step (1)

Your first task is to write a program which reads this file into two
parallel arrays in memory. One array contains the titles, and the
other array contains the authors. The arrays are 'parallel' in the
sense that the n-th element of the authors array contains the authors
associated with the n-th element of the titles array. Although the
data in the test file describes only a few books you should dimension
your arrays for an arbitrary number of books in the live data file
(which may be different from the test file). The size of the arrays
should be defined in your program by the declaration const int
ARRAY_SIZE = 1000.

For the purpose of this assignment (so as not to make it too
complicated) you should declare your two arrays at file scope (i.e.
outside any function and before the function main()). The declarations
should be as follows:

string bookTitle [ARRAY_SIZE];

string bookAuthor [ARRAY_SIZE];

Write a function whose signature is as follows:

int loadData (string pathname)

The function loadData should open the data file located in the path on
your hard disk specified by pathname. If it cannot open the file for
some reason, it should return a value of -1 as an error result.
Otherwise it should read the data in the file into the two parallel
arrays, and return the number of book records that it read.

Now write a function whose signature is as follows:

void showAll (int count)

The parameter count provides the number of books actually stored in
the database (i.e the value returned by the function loadData()).
showAll() should read the designated number of records in the parallel
arrays and display it on the screen in the order it appears in the
database, in single column format as follows (using the data in the
example file):

Objects First with Java (Barnes and Kolling)

Game Development Essentials (Novak)

The Game Maker's Apprentice (Overmars)

C++ Programming: From Problem Analysis... (Malik)

....

....

Audio for Games (Brandon)

Now test your work so far, by writing a main() function to prompt the
user for the path to the data file, open and read the file to the
parallel arrays using the function loadData() (providing error
messages and recovery as appropriate if the file doesn't exist). Then
display the contents of the file using showAll().

Step (2)

Extend your program into a menu driven application to query the
database. When the program starts it should ask the user for the path
to the data file, and read the data into the parallel arrays, as in
step 1.

Then it should enter a loop, in each iteration of which it prompts the
user for what they want to do. The actions are as follows depending on
how the user responds to the prompt. See the sample dialog below to
see how this works in practice.

Q or q: Quit. The program terminates.

A or a: Search by Author. The program displays all records which
contain the designated text somewhere in the author field. You should
use a function with the following signature for this purpose: int
showBooksByAuthor (int count, string name). This function should
display all books whose author field contains the string passed in the
parameter called name. It returns the number of books displayed. count
specifies the number of books in the database.

T or t: Search by Title. The program displays all records which
contain the designated text somewhere in the title field. You should
use a function with the following signature for this purpose: int
showBooksByTitle (int count, string title). This function should
display all books whose title field contains the string passed in the
parameter called title. It returns the number of books displayed.
count specifies the number of books in the database.

Sample Dialog

Note: In the example below, the user's input is in BOLD. The program
output is not.

Welcome to Colin's Library Database.

Please enter the name of the backup file: C:\library.txt

14 records loaded successfully.

Enter Q to Quit, A to search by Author, T to search by Title: A

Author's Name: Malik

C++ Programming: From Problem Analysis... (Malik)

C++ Programming: Program Design Including... (D. S. Malik)

2 records found.

Enter Q to Quit, A to search by Author, T to search by Title: A

Author's Name: Goble

0 records found.

Enter Q to Quit, A to search by Author, T to search by Title: T

Title: Game

Game Development Essentials (Novak)

The Game Maker's Apprentice (Overmars)

Game Character Development with Maya (Ward)

Developing Games in Java (Brackeen)

Audio for Games (Brandon)

5 records found.

Enter Q to Quit, A to search by Author, T to search by Title: Q

Mar 10 '07 #1
7 11748
ia*************@gmail.com wrote:
well i got this assignment which i dont even have a clue what i am
supposed to do. it is about reading me data from the file and load
them into a parallel array here is the question:
If you really don't have any clue I'm not sure how you think you are
going to get one by posting your assignment here. Have you not been
attending lessons? Did you get stuck at the beginning and never catch
up? Did you never ever really get it? Some people just aren't mentally
suited to programming, this might be when you find out.

More optimistically - first program is always difficult. The only answer
is to attempt to write some code. You're fortunate that you have some
detailed instructions. Follow them as best you can. When you have
written some code and you have *specific* questions, post the code with
the question and you'll get lots of help.

Failing that I believe there are sites on the internet where you can pay
to get your homework done. If you think that is a good idea then you
really are beyond hope.

john
Mar 10 '07 #2
i started with this code and i am trying to read file into the array
but i am not sure if it is right. By the way i want to learn how to do
it instead of getting it done so if you can help me i would really
appreciae it!

thanks!

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include<stdio.h>

using namespace std;

main()
{
// to read a database from the file:

FILE *fp;
fp=fopen("test.txt","rb");

//parallel array:

const int MAX= 100;
string name[MAX]; //name of author array
string book[MAX]; // name of the books array
int i;

//to reset the array elements to zero:

for (int i=0; i < MAX; i++)
{
name[i]=0;
book[i]=0;
}

Mar 10 '07 #3
ia*************@gmail.com wrote:
i started with this code and i am trying to read file into the array
but i am not sure if it is right. By the way i want to learn how to do
it instead of getting it done so if you can help me i would really
appreciae it!

thanks!

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include<stdio.h>
unneeded. Use iosreams instead
>
using namespace std;

main()
implicit int is not supported in C++ use int main()
{
// to read a database from the file:

FILE *fp;
fp=fopen("test.txt","rb");
// use an IOStream rather than an FP
>
//parallel array:

const int MAX= 100;
string name[MAX]; //name of author array
string book[MAX]; // name of the books array
use a struct, rather than separate arrays. The struct should contain
two elements (you should be able to figure out what they are).
use a vector of said struct, rather than a fixed size. What happens if
there's more than 100 entries in the file?
int i;

//to reset the array elements to zero:

for (int i=0; i < MAX; i++)
{
name[i]=0;
book[i]=0;
unneeded, they strings in the array are default initialized.
}
And?
Mar 11 '07 #4
Let me expound a little bit on one of my comments.

I told you to use iostreams and forget stdio and FILE*.

You have correctly chosen to use std::string for your strings, instead
of a char array. This is a *GOOD THING*. However, stdio doesn't know
how to properly handle std::strings. Iostreams, on the other hand, do
know how to deal with std::string.

Therefore you should forget about C-style I/O (stdio) and use C++ style
I/O (IOStreams).


Mar 11 '07 #5
ia*************@gmail.com wrote:
i started with this code and i am trying to read file into the array
but i am not sure if it is right. By the way i want to learn how to do
it instead of getting it done so if you can help me i would really
appreciae it!

thanks!

#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include<stdio.h>

using namespace std;

main()
{
// to read a database from the file:

FILE *fp;
fp=fopen("test.txt","rb");

//parallel array:

const int MAX= 100;
string name[MAX]; //name of author array
string book[MAX]; // name of the books array
Straight away you have done something that your assignment said you
should not do.

Go back to you assignment, re-read that part where it says 'declare your
two arrays at file scope' and 'The declarations should be as follows'.

Then read the part that says 'Now write a function', start with that
part don't start with main, leave main until you get to the part that
says 'Now test your work so far'.

Your professor has obviously made an effort to make this assignment as
easy as possible, you should at least follow his instructions.

john
Mar 11 '07 #6
>
use a struct, rather than separate arrays. The struct should contain
two elements (you should be able to figure out what they are).
use a vector of said struct, rather than a fixed size. What happens if
there's more than 100 entries in the file?

His assignment specifically says use parallel fixed size arrays.

john
Mar 11 '07 #7
red floyd <no*****@here.dudewrote:

>const int MAX= 100;
string name[MAX]; //name of author array
string book[MAX]; // name of the books array
use a struct, rather than separate arrays.
I'd agree with that, except that the assignment specifically said to
use parallel arrays.

--
Tim Slattery
Sl********@bls.gov
http://members.cox.net/slatteryt
Mar 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dariusz | last post by:
Below is part of a code I have for a database. While the database table is created correctly (if it doesn't exist), and data is input correctly into the database when executed, I have a problem...
2
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
4
by: Andy | last post by:
Hello All: I have a field in the database that is an Image. I have no idea how the data is stored in here (Image, compressed, encrypted, plain text, etc). I am trying to write the contents to...
1
by: Mr. B | last post by:
VB.net 2003 c/w Framework 1.1 and MS Access db We have a commercial program that does our Acounting and Time Sheets (Timberline). At least once a day our Accounting department runs a Script...
18
by: John | last post by:
Hi, I'm a beginner is using C# and .net. I have big legacy files that stores various values (ints, bytes, strings) and want to read them into a C# programme so that I can store them in a...
9
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt,...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.