Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

El-Jebus

macrumors newbie
Original poster
Mar 16, 2011
2
0
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:
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!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
I have no idea if this works, but it compiles now:
Code:
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>

using namespace std;

// Functions
void showAll(struct Book [], int);
int loadData(string);
//void sortByAuthor(int);
//void sortByTitle(int);

//Struct
struct Book{
	string title;
	string author;
};
const int ARRAY_SIZE = 1000;
struct 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, 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(struct Book books[], 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;
	}
	
}

It looked like you were trying to turn an array of Books into two arrays of strings by trying to access the "element" of the array, which doesn't exist. This doesn't work, obviously. I think it might in later versions of fortran, but i can't remember. In any event, if you just pass the whole structure to showAll (it's global anyway, so you really don't need to pass anything...) it should work fine.

-Lee

EDIT: I remembered right, fortran allows some nice shortcuts to elements of a type in an array:
Code:
program testme
  implicit none
  type testType
    integer :: theInt
    integer :: theSecondInt
    integer :: theThirdInt
  end type
  type (testType) :: myList(100)
  integer :: intList(100)

  myList%theInt = 5
  myList%theSecondInt = 7
  myList%theThirdInt = 2

  intList = myList%theThirdInt
  write(7,*) ' The list is: ', intList(1:100)

end program testme
 
Last edited:

MaynardJames

macrumors newbie
Aug 27, 2008
18
0
I compiled your code, and got a few different errors.

Code:
if(choice == 's' || choice == 'S'){
        showAll(books.title, books.author, linenum);

Here we get the errors:
"request for member ‘title’ in ‘books’, which is of non-class type ‘Book [1000]’"
"request for member ‘author’ in ‘books’, which is of non-class type ‘Book [1000]’"

What is books exactly? What is it's type? How are you trying to use it here? Is that correct?

Code:
void showAll(string books[].title, string books[].author, int linenum){

Here we get the error:
"expected ‘,’ or ‘...’ before ‘.’ token"

What are the first two arguments in showAll? What does string in the argument represent? What is books[].title supposed to represent? Do they match up with the function prototype?

Code:
   cout << books[i].author << "(" << books[i].author <<")" << endl;

Here we ge the errors
"‘struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ has no member named ‘author’"
"‘struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ has no member named ‘author’"

This goes back to the last error. When you are trying to print out books, what exactly is books ? What is its type? Why do you suppose the compiler is telling you that it has no member named author?
 

El-Jebus

macrumors newbie
Original poster
Mar 16, 2011
2
0
Awesome! Thank you Lee! That is exactly what I couldn't wrap my head around.

Now I just need to figure out how to search within the struct!

It would help if you told us what the errors were.

I'll definitely do that next time, thanks!
 

RiskyMr

macrumors newbie
Apr 8, 2009
27
0
Note that the clause (choice != 'q' || choice != 'Q') will always evaluate to true.
 

SidBala

macrumors 6502a
Jun 27, 2010
533
0
Note that the clause (choice != 'q' || choice != 'Q') will always evaluate to true.

I am guessing he noticed that during execution and then did this:

Code:
	while(choice != 'q' || choice != 'Q'){
		if(choice == 'q' || choice == 'Q'){
			return 0;
 

RiskyMr

macrumors newbie
Apr 8, 2009
27
0
I am guessing he noticed that during execution and then did this:

Code:
	while(choice != 'q' || choice != 'Q'){
		if(choice == 'q' || choice == 'Q'){
			return 0;


Indeed. In that case he probably intended:
Code:
while(choice != 'q' && choice != 'Q'){
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.