Errors in C++ struct code

Discussion in 'Mac Programming' started by El-Jebus, Mar 16, 2011.

  1. El-Jebus macrumors newbie

    Joined:
    Mar 16, 2011
    #1
    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!
     
  2. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #2
    It would help if you told us what the errors were.
     
  3. lee1210, Mar 16, 2011
    Last edited: Mar 16, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    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
    
     
  4. MaynardJames macrumors newbie

    Joined:
    Aug 27, 2008
    #4
    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?
     
  5. El-Jebus thread starter macrumors newbie

    Joined:
    Mar 16, 2011
    #5
    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!

    I'll definitely do that next time, thanks!
     
  6. RiskyMr macrumors newbie

    Joined:
    Apr 8, 2009
    #6
    Note that the clause (choice != 'q' || choice != 'Q') will always evaluate to true.
     
  7. SidBala macrumors 6502a

    Joined:
    Jun 27, 2010
    #7
    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;
     
  8. RiskyMr macrumors newbie

    Joined:
    Apr 8, 2009
    #8

    Indeed. In that case he probably intended:
    Code:
    while(choice != 'q' && choice != 'Q'){
    
     

Share This Page