473,748 Members | 3,604 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
showBooksByAuth or (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
showBooksByTitl e (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 11788
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.d udewrote:

>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
3025
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 when reading out the data into the PHP variables / array. It should be displaying the information out in the following way, using the PHP array: n_date, n_headline, n_summarytext, n_fulltext
2
10697
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 appears to be in the OLEDB driver also. My connection was: conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strPath & ";" & "Extended Properties='Text;HDR=NO;FMT=Delimited'"
3
9511
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 the end of this message is the inflate method this is where I get stuck I know that I need a byte array but because I am decompressing a string I have no idea of how big the byte array will need to be in the end (the inflate and deflate methods...
4
7625
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 a text file, image file, etc so I can see if the data is stored in a way we can understand (we have been tasked to write an app and the app needs to read this field, but we don't know what it really contains). How would I go about reading the...
1
1571
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 file that Exports info into an MS Access db file and which has 7 Tables (stuff like Project info, Project numbers, User names, etc). I wrote a Time Sheet entry application to read this info and to allow Employees to write their Time Sheet data into...
18
9070
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 database. The files are written by a late 1980's PC Pascal programme, for which I don't have the source code. I've managed to reverse engineer the file format. The strings are stored as Ascii in the file, with the first byte indicating the string...
9
2100
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, etc. Within the procedure I also validate that the file loaded to the server was loaded within the current day. Everything has run smooth for quite some time using this method. Now the vendor has told me he has choice, but to the send the file...
0
2194
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 CSV file without any issues only if all the fields are not null. If any field is blank, it moves the values to the left and displays the value under invalid column. Example is shown below: A part of CSV file. I am reading the first row as...
21
3062
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 00000000_00 0 000 000 000 0000 000 I need to read the file line by line and eventually parse out each piece of the file and store in arrays that correspond to the specific
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.