Hopefully someone here can help me! I thought I understood how to use structs, but I guess I was wrong. The code is supposed to open a .txt file and read it to the struct so you can then show the whole thing or search for specific lines. In this case it is a library document that holds the title of the book on the first line and the name of the author on the second, and it is supposed to output the title of the book followed by the author name in parenthesis on the same line. I am getting a few errors related to calling the variables. Here is the code:
Thank you for any help you can give me!
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
// Functions
void showAll(string[], string[], int);
int loadData(string);
//void sortByAuthor(int);
//void sortByTitle(int);
//Struct
struct Book{
string title;
string author;
};
const int ARRAY_SIZE = 1000;
Book books [ARRAY_SIZE];
string files;
string word;
int bookcount;
int linenum = 0;
int i = 0;
char delim = '\n';
// Main function
int main(int argc, char * const argv[]){
loadData(files);
char choice;
cout << "Welcome to Jebus' Library Lister!" << endl;
cout << "Please select from the list below." << endl;
cout << "To quit, press Q. To show complete list of books, press S." << endl;
cout << "To search, press A for Author or T for Title. Please enter your selection: ";
cin >> choice;
while(choice != 'q' || choice != 'Q'){
if(choice == 'q' || choice == 'Q'){
return 0;
}else{
if(choice == 's' || choice == 'S'){
showAll(books.title, books.author, linenum);
}else{
cout << "I'm sorry, that was an invalid input." << endl;
cout << "Please try again: ((Q)uit, (S)how all, (A)uthor, (T)itle) ";
cin >> choice;
}
// Add options for Author and Title search
}
cout << endl;
cout << "To quit, press Q. To show complete list of books, press S." << endl;
cout << "To search, press A for Author or T for Title. Please enter your selection: ";
cin >> choice;
}
}
int loadData(string files){
// Variables declaration
ifstream library;
char filename[ARRAY_SIZE];
string line;
// Open the file after placing the file where the program will read it
cout << "Please enter the filepath: ";
cin.getline(filename, 256);
library.open(filename);
while(library.fail()){
char buffer[ARRAY_SIZE];
cout << "Current directory is: " << getcwd(buffer, 1000) << "\n";
cout << "\nError: No such file exists. Please try again: ";
cin.getline(filename, 256);
library.open(filename);
}
// Give data to arrays
while(!library.eof()){
getline(library, line);
line[line.length() - 1] = ' ';
books[linenum].title = line;
getline(library, line);
line[line.length() - 1] = ' ';
books[linenum].author = line;
linenum++;
}
library.close();
return linenum;
}
// Print list of books in library
void showAll(string books[].title, string books[].author, int linenum){
cout << "The books currently in the library are: " << endl;
for(i = 0; i < linenum; i++){
cout << books[i].author << "(" << books[i].author <<")" << endl;
}
}
Thank you for any help you can give me!