473,289 Members | 1,848 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,289 software developers and data experts.

Searching parallel arrays

My program is designed to take input from a file containing a list of titles and authors. The file looks like so:

title
associated author
next title
associated author
etc.

The problem I'm having is with my showBooksByTitle and showBooksByAuthor functions, I guess what I'm asking is how does one compare parts of strings so that if a user searched for Mal (and there was author D.S. Malik and just Malik) both would show up?

right now this code returns only exact matches and also prints an empty newline and a new line with some spaces and a ().

Of course any help is greatly appreciated. This is my first year programming. I've included the whole code just to be safe that I'm not leaving out anything that could be the problem.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<cstring>
  5.  
  6. using namespace std;
  7.  
  8. struct Book {
  9.        string title;
  10.        string author;
  11. };
  12.  
  13. const int ARRAY_SIZE = 1000;
  14. Book books [ARRAY_SIZE];
  15.  
  16. int loadData (string);
  17. void showAll (int);
  18. int showBooksByAuthor (int, string);
  19. int showBooksByTitle (int, string);
  20.  
  21. int main() {
  22.     //Declare variables
  23.     string pathname;
  24.     string title;
  25.     string name;
  26.     string word;
  27.     int count;
  28.     char response;
  29.  
  30.     //ask user for pathname
  31.     cout << "What is the path of the library file? ";
  32.     cin >> pathname;
  33.     cout << endl;
  34.     count = loadData(pathname);
  35.  
  36.     //input data into arrays
  37.     loadData(pathname);
  38.     cout << endl << count << " records loaded successfully." << endl << endl;
  39.  
  40.     //Show user menu    
  41.     cout << "Please enter Q to Quit, A to search for the Author, T to search for the Title, "
  42.          << endl << "or S to Show all: ";    
  43.     cin >> response;
  44.  
  45.     switch(response) {
  46.          case 'q':
  47.               break;
  48.          case 'Q':
  49.               break;
  50.          case 'a':
  51.               cout << endl << "Please enter author's name: ";
  52.               cin >> name;
  53.               showBooksByAuthor(count, name);
  54.               break;
  55.          case 'A':
  56.               cout << endl << "Please enter author's name: ";
  57.               cin >> name;
  58.               showBooksByAuthor(count, name);
  59.               break;
  60.          case 't':
  61.               cout << endl << "Please enter all or part of the title: ";
  62.               cin >> title;
  63.               showBooksByTitle(count, title);
  64.               break;
  65.          case 'T':
  66.               cout << endl << "Please enter all or part of the title: ";
  67.               cin >> title;
  68.               showBooksByTitle(count, title);
  69.               break;
  70.          case 's':
  71.               cout << endl;
  72.               showAll(count);
  73.               break;
  74.          case 'S':
  75.               cout << endl;
  76.               showAll(count);
  77.               break;
  78.          default:
  79.                  cout << endl << "Invaled input, please try again: ";
  80.                  break;
  81.     }
  82.  
  83.     //pause and exit
  84.     cout << endl;
  85.     system("PAUSE");
  86.     return 0;
  87. }
  88.  
  89.  
  90. int loadData(string pathname) {
  91.     int i = 0;
  92.     int j = 0; 
  93.     ifstream library;
  94.  
  95.     //open file, if not successful, output error message
  96.     library.open(pathname.c_str());
  97.     if (!library.is_open()) {
  98.        cout << "Unable to open input file." << endl;
  99.        return -1;
  100.     }
  101.     //reads title and author from file into designated string
  102.     //this is assuming title comes first and author comes after
  103.     while(!library.eof()) {
  104.             getline(library, books[i].title);  
  105.             getline(library, books[i].author);
  106.             i++;
  107.     }
  108.     return i;
  109. }
  110.  
  111. void showAll (int count) {
  112.      for (int i = 0; i < count; i++) {
  113.          cout << books[i].title << " (" << books[i].author << ")" << endl;
  114.      }     
  115. }
  116.  
  117. int showBooksByAuthor(int count, string name) {
  118.     int found;
  119.     for(int n = 0; n < 28; n++) {
  120.         found = name.find(books[n].author);
  121.         if(found != string::npos) {
  122.            cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
  123.         }
  124.     }
  125.     return 0;
  126. }
  127.  
  128.  
  129. int showBooksByTitle (int count, string title) {
  130.         int found;
  131.     for(int n = 0; n < 28; n++) {
  132.         found = title.find(books[n].title);
  133.         if(found !=string::npos) {
  134.            cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
  135.         }
  136.     }
  137.     return 0;
  138. }
  139.  
Attached Files
File Type: txt library.txt (604 Bytes, 439 views)
Dec 10 '11 #1

✓ answered by weaknessforcats

You are using C++ string objects. If your string name contains "Mickey Mouse" then string::find() should locate "key".

2 4119
weaknessforcats
9,208 Expert Mod 8TB
You are using C++ string objects. If your string name contains "Mickey Mouse" then string::find() should locate "key".
Dec 11 '11 #2
Thank you so much! I was using that operation but I had it backwards.
Dec 11 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Bryce Maschino | last post by:
hello, i am trying to make a java program that will take several different arrays and search through them in a loop suchs as: for (i=0; i < 5; i++) { if (firstarray<such and such { blah blah
6
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
3
by: cpuracr8 | last post by:
i've been looking through the topics here and i can't quite find one that helps me. i'm trying to sort a struct that contains three arrays using the sort() function. the three arrays are parallel...
12
by: lifeshortlivitup | last post by:
I am trying to construct two, one-dimensional parallel arrays that will give me a letter grade for whatever score I enter. The two arrays I need are minimum score and grade. I don't understand how...
1
by: BlackJackal | last post by:
Alright here is the problem I have for homework. I understand most of it but I am not exactly sure what the problem is asking me to do or how to search the seperate arrays using the account number...
8
by: earla12 | last post by:
Hi, I'm working on a program that takes the following input from the user: Student Name, Student Number, and Student GPA The information is then supposed to be stored in three separate...
2
by: dejavu33 | last post by:
Hello! Im learning arrays and am having trouble with parallel arrays. I dont understand exactly what they are. Can anyone give me an explanation of what they are or maybe how they can be declared...
3
momotaro
by: momotaro | last post by:
am trying to count the accurence and the position of dots in an array and fill another array with the locations found: this is the code: void get_dot_pos(char *IP, int *pos) { int i = 0, j,...
1
by: joor | last post by:
Hi I am a beginner and currently trying to create a small program. I seem to be stuck on the multiplication of their elements. Eg. I have an Array with 4 different prices for 4 different...
9
by: drhowarddrfine | last post by:
I don't want to use a db manager, like mysql, for such a small database but I'm finding this trickier than I thought and hope someone can provide some guidance. I have a restaurant menu with...
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: 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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.