-
May 12th, 2012, 07:53 AM
#1
[RESOLVED] c++ data structures program to search records by different fields
I work in a small library and have been given a task to make a program. To make things short, i have copied all my records to a text file
Book name---------------Author
1.Lord of the rings,J. R R Tolkiens
2.Tom sawyer,,Mark twain
3.Just after sunset,Stephen king
4.The shining,Stephen king
5.Oliver twist,Charles Dickens
6.Huck Finn,Mark twain
7.Children on Hurin,J R R Tolkiens
8.Silmarilions,J R R tolkiens
How can i make my c++ program to read this file, read records as arrays, sort them, and search either by book name or by the auhtor. If i write Tolkiends, how can i make it output all the books written by J R R tolkiens?
-
May 12th, 2012, 08:00 AM
#2
Re: c++ data structures program to search records by different fields
what I have done is this
struct book // declare class
{
int no;
string author; // type name of string
string book; // type name of string
string genre; // type name of string
book* next; // type name of string
book()
{
no=0;
author=book=genre=" ";
next=NULL; // next given FALSE value
}
void setnext(book *t)
{
next=t;
}
book* getnext()
{
return next;
}
};
class linklist
{
book *start;
book *current;
public:
linklist()
{
start=NULL;
current=NULL;
}
void insert(book *tmp)
{
if(start==NULL)
{
start=tmp;
}
else
{
current=start;
while(current->getnext()!=NULL)
{
current=current->getnext();
}
current->setnext(tmp);
}
}
bool search_no(int value)
{
current=start;
while(current!=NULL)
{
if(current->no==value)
{
cout<<current->author<<endl;
cout<<current->book<<endl;
cout<<current->genre<<endl;
cout<<"---------------------------------------------- --------------------------"<<endl;
return true;
}
else
current=current->getnext();
}
return false;
}
-
May 12th, 2012, 08:18 AM
#3
Re: c++ data structures program to search records by different fields
First of all. When you post code please embed it in code tags to preserve indentation [code]Paste your code here[/code]
Second, since you use std::string you seem to be aware of stl so use a std::list instead of writing your own linked list.
Here are two good reference sites http://cplusplus.com/ & http://cppreference.com
-
May 12th, 2012, 08:19 AM
#4
Re: c++ data structures program to search records by different fields
Code:
what I have done is this
struct book // declare class
{
int no;
string author; // type name of string
string book; // type name of string
string genre; // type name of string
book* next; // type name of string
book()
{
no=0;
author=book=genre=" ";
next=NULL; // next given FALSE value
}
void setnext(book *t)
{
next=t;
}
book* getnext()
{
return next;
}
};
class linklist
{
book *start;
book *current;
public:
linklist()
{
start=NULL;
current=NULL;
}
void insert(book *tmp)
{
if(start==NULL)
{
start=tmp;
}
else
{
current=start;
while(current->getnext()!=NULL)
{
current=current->getnext();
}
current->setnext(tmp);
}
}
bool search_no(int value)
{
current=start;
while(current!=NULL)
{
if(current->no==value)
{
cout<<current->author<<endl;
cout<<current->book<<endl;
cout<<current->genre<<endl;
cout<<"---------------------------------------------- --------------------------"<<endl;
return true;
}
else
current=current->getnext();
}
return false;
}
-
May 12th, 2012, 08:23 AM
#5
Re: c++ data structures program to search records by different fields
My boss asks for atleast the file handling code till coming monday. Where as i have to submit the whole task ,on visual c++, by the end of next week! Sir, i dont have time to study the whole picture write now! Even much of this code i have taken help from source codes. Only if you could help me with this please, i will be able to complete my task!
-
May 12th, 2012, 08:54 AM
#6
Re: c++ data structures program to search records by different fields
Originally Posted by smrizvi1
I work in a small library and have been given a task to make a program.
...
How can i make my c++ program to read this file,
...
If this is really something you need for a 'small library', rather than a homework question, then just use a Spreadsheet or a Database. If you don't have one, you can get OpenOffice for free (http://www.openoffice.org/)
Even if you don't currently know how to use a Spreadsheet/Database to do this, it will be a lot easier to learn this than trying to learn C++ in a few days. And the resulting application is much more likely to be bug-free.
-
May 12th, 2012, 10:00 AM
#7
Re: c++ data structures program to search records by different fields
Originally Posted by smrizvi1
I work in a small library and have been given a task to make a program. To make things short, i have copied all my records to a text file
What if the book's title has a comma in it? How would you create an entry, given that you're using a comma to separate the title and the author?
How can i make my c++ program to read this file, read records as arrays,
Here is where you start -- write a function that when given one of those lines in your file, separates each of the field into 3 fields. For example:
Code:
#include <string>
struct BookInfo
{
std::string number;
std::string title;
std::string author;
};
BookInfo ParseRecord(const std::string& rec)
{
BookInfo b;
// break up rec and store data in b;
//...
return b;
}
int main()
{
BookInfo myInfo = ParseRecord("1.Lord of the rings,J. R R Tolkiens");
}
When you have written this function then let's talk about linked lists. If you don't have this function, then why even try to code anything else? This is the first function you should have written, as nothing else matters if you don't have the ability to take a string and create a BookInfo from it.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 12th, 2012 at 01:12 PM.
-
May 12th, 2012, 11:59 AM
#8
Re: c++ data structures program to search records by different fields
Originally Posted by smrizvi1
My boss asks for atleast the file handling code till coming monday. Where as i have to submit the whole task ,on visual c++, by the end of next week! Sir, i dont have time to study the whole picture write now! Even much of this code i have taken help from source codes. Only if you could help me with this please, i will be able to complete my task!
This forum frequently (and erroneously) gets taken for a sort of ambulance for half dead projects reanimation. In case you cannot drive a car, you just don't drive. If you have no time to study driving, you just don't drive. If you cannot do programming and have no time to study programming, you just don't do programming.
Best regards,
Igor
-
May 25th, 2012, 01:13 PM
#9
Re: [RESOLVED] c++ data structures program to search records by different fields
my task is to make a structure to store the record of library. The record must consist of at least following fields: Title, Author, Edition, Price, Publisher, Category.
-Define functions authorSearch ( ), TitleSearch ( ) and CategorySearch( ) to search a book with respect to author, title and category. [There can be more than one books, written by one author, in one category]
The program must be menu driven with the following choices:
1> Add new record
2> Search a book w.r.t author
3> Search a book w.r.t Title
4> Search a book w.r.t category
5> Exit
The program must be able to store at least 50 records.
plz help me
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|