c++ - Searching parallel arrays -
my program designed take input file containing list of titles , authors. file looks so:
title associated author next title associated author etc.
the problem i'm having showbooksbytitle , showbooksbyauthor functions. right code returns exact matches , prints empty newline , new line spaces , ().
of course appreciated. first year programming. i've included whole code safe i'm not leaving out 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 pathname cout << "what path of library file? "; cin >> pathname; cout << endl; count = loaddata(pathname); //input data arrays loaddata(pathname); cout << endl << count << " records loaded successfully." << endl << endl; //show user menu cout << "please enter q quit, search author, t search title, " << endl << "or s 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 or part of title: "; cin >> title; showbooksbytitle(count, title); break; case 't': cout << endl << "please enter or part of 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 , exit cout << endl; system("pause"); return 0; } int loaddata(string pathname) { int = 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 open input file." << endl; return -1; } //reads title , author file designated string //this assuming title comes first , author comes after while(!library.eof()) { getline(library, books[i].title); getline(library, books[i].author); i++; } return i; } void showall (int count) { (int = 0; < 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; }
the unexpected output because read data file wrong. streams eof flag not set until after tried operation on stream, loop iterates 1 many times.
change loop in loaddata
this:
while(getline(library, books[i].title) && getline(library, books[i].author)) i++;
this uses fact std::getline
return stream, , stream can used true/false value.
Comments
Post a Comment