By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

C++ reading database from the file into the array

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
>
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.