I am writing a program that stores file.txt strings into parallel arrays.
Once I have my arrays, create a loop that prompts the user for these following input commands:
Show: Prints arrays to the screen.
Author: User inputs author name, loop searches author array for input author name then prints book title and author name, as well as the total number of books found.
Title: User inputs title name, loop searches title array for input title name then prints book title and author name, as well as the total number of books found.
Quit: Exits program.
So here is where I am hung up. When the user goes to input author / title name, and the line includes spaces, the program goes bonkers. To fix that I tried using getline in my if statements but they werent working for some reason. Also the text file I am reading from has one author name listed in different ways.
example: Tolkien
J.R.R Tolkien
I want the search author function to find both books even if my user inputs
'tolkien'.
So:
1. Why am I having trouble using get line in my if statements?
2. How do I search array indices for a particular string?
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int ARRAY_SIZE = 1000;
string bookAuthor[ARRAY_SIZE];
string bookTitle[ARRAY_SIZE];
int main(){
char comIn = ' ';
ifstream actFile;
string userInAuthor;
string userInTitle;
string fileStr;
int bookCount = 0;
string authorStr;
string titleStr;
cout << "Welcome to the Library Database. " << endl
<< "Please enter the name of the backup file: " << endl;
getline(cin, fileStr);
actFile.open(fileStr);
// file open error
if(!actFile.is_open()){
return -1;
}
// ** SAVING FILE STRINGS INTO ARRAY INDEX **
while(!actFile.eof()){
getline(actFile, titleStr);
bookTitle[bookCount ] = titleStr;
getline(actFile, authorStr);
bookAuthor[bookCount ] = authorStr;
bookCount++;
}
// *********************************************
//display total records found from file read
cout << endl << bookCount <<
<< " records loaded successfully." << endl;
while(comIn != 'q' || comIn != 'Q') {
cout << endl << endl << "Enter Q to (Quit), Search
<< (A)uthor, Search (T)itle,"
<< " (S)how All: ";
cin >> comIn;
if(comIn == 'a' || comIn == 'A'){
cout << "Author's Name: ";
cin >> userInAuthor;
for(int a = 0; a < bookCount + 1; a++){
if(userInAuthor == bookAuthor[a]){
cout << endl << bookTitle[a] <<
<<"(" << bookAuthor[a] << ")";
}
}
}
if(comIn == 't' || comIn == 'T'){
cout << "Title's Name: ";
getline(cin, userInTitle);
for(int t = 0; t < bookCount + 1; t++){
if(userInTitle == bookTitle[t]){
cout << endl << bookTitle[t] << "(" <<
<< bookAuthor[t] << ")";
}
}
}
if(comIn == 's' || comIn == 'S'){
for(int s = 0; s < bookCount; s++){
cout << endl << bookAuthor[s] << endl <<
<< bookTitle[s];
}
}
if(comIn == 'q' || comIn == 'Q'){
return 0;
}
}
//pause and exit
getchar();
getchar();
return 0;
}
|