473,397 Members | 1,950 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,397 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, 440 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 4125
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.