2 Replies - 1563 Views - Last Post: 18 March 2013 - 06:21 AM Rate Topic: -----

#1 tessierny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 12-February 13

Searching Parallel Arrays string miscalculation

Posted 18 March 2013 - 04:50 AM

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. 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.

   #include <iostream>
    #include<string>
    #include<fstream>
    #include<cstring>
    
    using namespace std;
    
    struct Book {
        string title;
        string author;
    };
    
    const int ARRAY_SIZE = 1000;
    Book books [ARRAY_SIZE];
    
    int loadData (string);
    void showAll (int);
    int showBooksByAuthor (int, string);
    int showBooksByTitle (int, string);
    
    int main() {
        //Declare variables
        string pathname;
        string title;
        string name;
        string word;
        int count;
        char response;
        
        //ask user for pathname
        cout << "What is the path of the library file? ";
        cin >> pathname;
        cout << endl;
        count = loadData(pathname);
        
        //input data into arrays
        loadData(pathname);
        cout << endl << count << " records loaded successfully." << endl << endl;
        
        //Show user menu
        cout << "Please enter Q to Quit, A to search for the Author, T to search for the Title, "
        << endl << "or S to Show all: ";
        cin >> response;
        
        switch(response) {
            case 'q':
                break;
            case 'Q':
                break;
            case 'a':
                cout << endl << "Please enter author's name: ";
                cin >> name;
                showBooksByAuthor(count, name);
                break;
            case 'A':
                cout << endl << "Please enter author's name: ";
                cin >> name;
                showBooksByAuthor(count, name);
                break;
            case 't':
                cout << endl << "Please enter all or part of the title: ";
                cin >> title;
                showBooksByTitle(count, title);
                break;
            case 'T':
                cout << endl << "Please enter all or part of the title: ";
                cin >> title;
                showBooksByTitle(count, title);
                break;
            case 's':
                cout << endl;
                showAll(count);
                break;
            case 'S':
                cout << endl;
                showAll(count);
                break;
            default:
                cout << endl << "Invaled input, please try again: ";
                break;
        }
        
        //pause and exit
        cout << endl;
        system("PAUSE");
        return 0;
    }
    
    
    int loadData(string pathname) {
        int i = 0;
        int j = 0;
        ifstream library;
        
        //open file, if not successful, output error message
        library.open(pathname.c_str());
        if (!library.is_open()) {
            cout << "Unable to open input file." << endl;
            return -1;
        }
        //reads title and author from file into designated string
        //this is assuming title comes first and author comes after
        while(!library.eof()) {
            getline(library, books[i].title);
            getline(library, books[i].author);
            i++;
        }
        return i;
    }
    
    void showAll (int count) {
        for (int i = 0; i < count; i++) {
            cout << books[i].title << " (" << books[i].author << ")" << endl;
        }
    }
    
    int showBooksByAuthor(int count, string name) {
        int found;
        for(int n = 0; n < 28; n++) {
            found = name.find(books[n].author);
            if(found != string::npos) {
                cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
            }
        }
        return 0;
    }
    
    
    int showBooksByTitle (int count, string title) {
        int found;
        for(int n = 0; n < 28; n++) {
            found = title.find(books[n].title);
            if(found !=string::npos) {
                cout << endl << books[n].title << " (" << books[n].author << ")" << endl;
            }
        }
        return 0;
    }

This post has been edited by tessierny: 18 March 2013 - 04:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Searching Parallel Arrays string miscalculation

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: Searching Parallel Arrays string miscalculation

Posted 18 March 2013 - 05:19 AM

Didn't you get the answer to this question back in 2011?

http://bytes.com/top...parallel-arrays
Was This Post Helpful? 0
  • +
  • -

#3 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Searching Parallel Arrays string miscalculation

Posted 18 March 2013 - 06:21 AM

What I've found useful, is to use a binary search - if the target data is found in the array, great - show that. If the target data is NOT found, then return the nearest index, and when it is received by the calling function, then it will display the closest values to the target value, say 4 records less, and 4 records more.

Here's one way to do it. The binary search function usually returns the index of the found target data, and -1 otherwise. Change the -1 to be the negative index of the last array record data (which should be the closest one to the target, since a binary search "zeroes in" on the target, closer and closer. Now the negative number tells the calling function that started the search, that the target value wasn't found, and the negative number (the index) * -1, tells the calling function the closest values. Now use a for loop:

for(i=returned index * -1 - 4; i<returned index *-1 +4;i++)
   printf("Target data not found, closest is: \n %s   %s \n",title, authorsName);



VERY handy!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1