Seemingly Random Crashes @ array input
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Seemingly Random Crashes @ array input

  1. #1
    Join Date
    Apr 2010
    Posts
    5

    Seemingly Random Crashes @ array input

    Hello everyone.

    I'm writing a program to store book data, the program is done for the most part, however it crashes at seemingly random places when I'm inputting data into the arrays.

    Yes, this is for an assignment, however this is a problem not even my instructor could identify and help me fix. The code from the program is completely finished except for this error.

    I'm using 3-dim arrays to store this data. I'm really not sure where the problem is in the code. Here's an example of inputting some test data, the last line here is a complete crash (without a popup however). The program just quits working without notice after I entered the third ISBN at the bottom of the screenshot. The place is quits working at varies with different test data.

    http://img256.imageshack.us/img256/2088/testcrash.png

    Considering that I have no idea where the error is, I figured it best to put the whole program here.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstring>
    
    using namespace std;
    bool moredata;
    char displaymenuchoice();
    void shouldcontinue();
    void addnewbook(int *** data, char *** info, float ** prices, int count);
    int findcheapest(float ** prices, int count);
    void displaycheapest(int *** data, char *** info, float ** prices, int count);
    int findmostpages(int *** data, char *** info, float ** prices, int count);
    void displaymostpages(int *** data, char *** info, float ** prices, int count);
    void displayall(int *** data, char *** info, float ** prices, int count);
    int findoldestbook(int *** data, int count);
    void displayoldest(int *** data, char *** info, float ** prices, int count);
    void sortbyauthor(int *** data, char *** info, float ** prices, int count);
    int getisbn();
    char * getauthor();
    char * gettitle();
    char * getpublisher();
    int getedition();
    int getyear();
    int getpages();
    int getcopies();
    float getmsrp();
    float getdiscount();
    int *** declaredata();
    char *** declareinfo();
    float ** declareprices();
    void printonebook(int *** data, char *** info, float ** prices, int count, int index);
    int printbyyear(int *** data, char *** info, float ** prices, int count);
    void printheader();
    
    int main()
    {
    	int count = 0;
    
    	int *** data = declaredata();
    	char *** info = declareinfo();
    	float ** prices = declareprices();
    
    	do
    	{
    		char choice = displaymenuchoice();
    		if(choice == 'A')
    		{
    			addnewbook(data, info, prices, count);
    			count++;
    		}
    		else if(choice == 'C')
    			displaycheapest(data, info, prices, count);
    		else if(choice == 'D')
    			displayall(data, info, prices, count);
    		else if(choice == 'M')
    			findmostpages(data, info, prices, count);
    		else if(choice == 'O')
    			displayoldest(data, info, prices, count);
    		else if(choice == 'S')
    			sortbyauthor(data, info, prices, count);
    		else if(choice == 'Y')
    			printbyyear(data, info, prices, count);
    
    		shouldcontinue();
    
    	
    	}while(moredata);
    
    
    	return 0;
    }
    
    int *** declaredata()
    {
    	int *** data = new int **[25];
    	for(int z=0; z<25; z++)
    	{
    		data[z] = new int *[25];
    		for(int r=0; r<18; r++)
    			data[z][r] = new int[5];
    	}
    
    	return data;
    }
    
    char *** declareinfo()
    {
    	char *** info = new char ** [25];
    	for(int i=0; i<25; i++)
    	{
    		info[i] = new char * [5];
    		for(int j=0; j<5; j++)
    			info[i][j] = new char[50];
    	}
    
    	return info;
    }
    
    float ** declareprices()
    {
    	float ** prices = new float * [25];
    	for(int p=0; p<25; p++)
    		prices[p] = new float [4];
    
    	return prices;
    }
    
    char displaymenuchoice()
    {
    	char tempchoice;
    
    	cout << endl;
    	cout << "Please enter your selection" << endl;
    	cout << "(A) : Add a new book" << endl;
    	cout << "(C) : Find the cheapest book" << endl;
    	cout << "(D) : Display all books" << endl;
    	cout << "(M) : Find book with the most pages" << endl;
    	cout << "(O) : Find the oldest book" << endl;
    	cout << "(S) : Sort by author, and then print sorted books" << endl;
    	cout << "(Y) : Display books by year" << endl;
    	cout << endl;
    
    	cin >> tempchoice;
    	cin.ignore();
    	return toupper(tempchoice);
    }
    
    void addnewbook(int *** data, char *** info, float ** prices, int count)
    {
    	data[count][0][0] = getisbn();
    	strcpy(info[count][0], getauthor());
    	strcpy(info[count][2], gettitle());
    	strcpy(info[count][1], getpublisher());
    	data[count][0][1] = getedition();
    	data[count][0][2] = getyear();
    	data[count][0][3] = getpages();
    	prices[count][0] = getmsrp();
    	prices[count][1] = getdiscount();
    	data[count][0][4] = getcopies();
    }
    
    int getisbn()
    {
    	int tempisbn = 0;
    	cout << "Enter ISBN: ";
    	cin >> tempisbn;
    	cin.ignore();
    	return tempisbn;
    }
    
    char * getauthor()
    {
    	char * tempauthor = new char;
    	cout << "Enter author: ";
    	cin.getline(tempauthor, 40); 
    	return tempauthor;
    }
    
    char * gettitle()
    {
    	char * temptitle = new char;
    	cout << "Enter title: ";
    	cin.getline(temptitle, 50); 
    	return temptitle;
    }
    
    char * getpublisher()
    {
    	char * temppub = new char;
    	cout << "Enter publisher: ";
    	cin.getline(temppub, 40);
    	return temppub;
    }
    
    int getedition()
    {
    	int temped = 0;
    	cout << "Enter edition: ";
    	cin >> temped;
    	cin.ignore();
    	return temped;
    }
    
    int getyear()
    {
    	int tempyear = 0;
    	cout << "Enter year: ";
    	cin >> tempyear;
    	cin.ignore();
    	return tempyear;
    }
    
    int getpages()
    {
    	int temppages = 0;
    	cout << "Enter pages: ";
    	cin >> temppages;
    	cin.ignore();
    	return temppages;
    }
    
    int getcopies()
    {
    	int tempcopies = 0;
    	cout << "Enter copies: ";
    	cin >> tempcopies;
    	cin.ignore();
    	return tempcopies;
    }
    
    
    float getmsrp()
    {
    	float tempmsrp = 0;
    	cout << "Enter MSRP: ";
    	cin >> tempmsrp;
    	cin.ignore();
    	return tempmsrp;
    }
    
    float getdiscount()
    {
    	float tempdisc = 0;
    	cout << "Enter discount price: ";
    	cin >> tempdisc;
    	cin.ignore();
    	return tempdisc;
    }
    
    void printheader()
    {
    	cout << endl;
    	cout << left << setw(10) << "ISBN" << setw(20) << "Author" << setw(45) << "Title" << setw(30) << "Publisher";
    	cout << setw(10) << "Edition" << setw(10) << "Year" << setw(10) << "Pages";
    	cout << setw(10) << "MSRP" << setw(10) << "Discount" << setw(10) << "Copies" << endl;
    }
    
    void printonebook(int *** data, char *** info, float ** prices, int count, int index)
    {
    
    	cout << left << setw(10) << data[index][0][0] << setw(20) << info[index][0] << setw(45) << info[index][2] << setw(30) << info[index][1];
    	cout << setw(10) << data[index][0][1] << setw(10) << data[index][0][2] << setw(10) << data[index][0][3];
    	cout << setw(10) << prices[index][0] << setw(10) << prices[index][1] << setw(10) << data[index][0][4];
    }
    
    void displaycheapest(int *** data, char *** info, float ** prices, int count)
    {
    	int index = 0;
    	index = findcheapest(prices, count);
    	printheader();
    	printonebook(data, info, prices, count, index);
    }
    
    int findcheapest(float ** prices, int count)
    {
    	int indexofbook = 0;
    	for (int booknum=0; booknum<count-1; booknum++)
    		if(prices[indexofbook][1] > prices[booknum][1])
    			indexofbook = booknum;
    	return indexofbook;
    }
    
    void displayall(int *** data, char *** info, float ** prices, int count)
    {
    	printheader();
    	for(int display=0; display<count; display++)
    	{
    		printonebook(data, info, prices, count, display);
    	}
    }
    
    int findmostpages(int *** data, char *** info, float ** prices, int count)
    {
    	int numofpages = 0;
    	int indexofmostpages = 0;
    
    	cout << "Enter page number to search by: ";
    	cin >> numofpages;
    	cin.ignore();
    
    	printheader();
    	for(int pages=0; pages<count; pages++)
    		if(numofpages < data[pages][0][3])
    		{
    			indexofmostpages = pages;
    			printonebook(data, info, prices, count, indexofmostpages);
    		}
    
    	return indexofmostpages;
    }
    
    int printbyyear(int *** data, char *** info, float ** prices, int count)
    {
    	int enteredyear = 0;
    	int indexofyear = 0;
    
    	cout << "Enter the year to sort by: ";
    	cin >> enteredyear;
    	cin.ignore();
    
    	printheader();
    	for(int year=0; year<count; year++)
    		if(enteredyear == data[year][0][2])
    		{
    			indexofyear = year;
    			printonebook(data, info, prices, count, indexofyear);
    		}
    
    	return indexofyear;
    }
    
    void displayoldest(int *** data, char *** info, float ** prices, int count)
    {
    	int oldestindex = 0;
    	oldestindex = findoldestbook(data, count);
    	printheader();
    	printonebook(data, info, prices, count, oldestindex);
    }
    
    int findoldestbook(int *** data, int count)
    {
    	int indexofoldest = 0;
    	for(int old=0; old<count; old++)
    		if(data[indexofoldest][0][2] > data[old][0][2])
    			indexofoldest = old;
    
    	return indexofoldest;
    }
    
    void sortbyauthor(int *** data, char *** info, float ** prices, int count)
    {
    	for(int iteration=1; iteration<count; iteration++)
    		for(int index=0; index<count-iteration; index++)
    			if(strcmp(info[index][0], info[index+1][0]) == 1)
    			{
    				char * author = new char[40];
    				strcpy(author, info[index][0]);
    				strcpy(info[index][0], info[index+1][0]);
    				strcpy(info[index+1][0], author);
    
    				char * publisher = new char[40];
    				strcpy(publisher, info[index][1]);
    				strcpy(info[index][1], info[index+1][1]);
    				strcpy(info[index+1][1], publisher);
    
    				char * title = new char[40];
    				strcpy(title, info[index][2]);
    				strcpy(info[index][2], info[index+1][2]);
    				strcpy(info[index+1][2], title);
    
    				int isbn = data[index][0][0];
    				data[index][0][0] = data[index+1][0][0];
    				data[index+1][0][0] = isbn;
    
    				int edition = data[index][0][1];
    				data[index][0][1] = data[index+1][0][1];
    				data[index+1][0][1] = edition;
    
    				int year = data[index][0][2];
    				data[index][0][2] = data[index+1][0][2];
    				data[index+1][0][2] = year;
    
    				int pages = data[index][0][3];
    				data[index][0][3] = data[index+1][0][3];
    				data[index+1][0][3] = pages;
    
    				int copies = data[index][0][4];
    				data[index][0][4] = data[index+1][0][4];
    				data[index+1][0][4] = copies;
    
    				float msrp = prices[index][0];
    				prices[index][0] = prices[index+1][0];
    				prices[index+1][0] = msrp;
    
    				float discountprice = prices[index][1];
    				prices[index][1] = prices[index+1][1];
    				prices[index+1][1] = discountprice;
    			}
    
    			displayall(data, info, prices, count);
    }
    
    void shouldcontinue()
    {
    	char yesorno;
    	
    	cout << "Do you want to continue? Y or N ";
    	cin >> yesorno;
    
    	if(toupper(yesorno) == 'Y')
    		moredata = true;
    	else
    		moredata = false;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    You need a new teacher.

    Code:
    	char * tempauthor = new char;
    	cout << "Enter author: ";
    	cin.getline(tempauthor, 40); 
    	return tempauthor;
    How many characters are you allocating for tempauthor vs. how many are you accepting for input?

    FWIW I would really recommend avoid three dimensional arrays and char arrays.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Seemingly Random Crashes @ array input

    Ouch. Way too many *s all over the place. That's almost painful to look at.....

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    Quote Originally Posted by Lindley View Post
    Ouch. Way too many *s all over the place. That's almost painful to look at.....
    I was thinking about this more. OP, why do you need three dimensional arrays? A single array of structs seems like it would suffice and would be a whole lot easier to maintain. No serious programmer would write code like that. I hope that's not what you're being taught.

  5. #5
    Join Date
    Apr 2010
    Posts
    5

    Re: Seemingly Random Crashes @ array input

    Thanks for your reply.

    The longest tempauthor would receive for the test data is 15 characters. I changed it to reflect this:

    Code:
    	char * tempauthor = new char;
    	cout << "Enter author: ";
    	cin.getline(tempauthor, 16); 
    	return tempauthor;
    Unless I misunderstood your point and it needs to match what was declared in the 3dim array it's entering into (in this case, 50). I tried it both ways and it still crashes at the same spot (with the same test data).

  6. #6
    Join Date
    Apr 2010
    Posts
    5

    Re: Seemingly Random Crashes @ array input

    Quote Originally Posted by ayjis View Post
    Thanks for your reply.

    The longest tempauthor would receive for the test data is 15 characters. I changed it to reflect this:

    Code:
    	char * tempauthor = new char;
    	cout << "Enter author: ";
    	cin.getline(tempauthor, 16); 
    	return tempauthor;
    Unless I misunderstood your point and it needs to match what was declared in the 3dim array it's entering into (in this case, 50). I tried it both ways and it still crashes at the same spot (with the same test data).

    @GCDEF, Lindley:
    My professor requires it this way. In order to teach us pointers, we make every array a pointer. This assignment also specifies we use 3-dim arrays.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    Quote Originally Posted by ayjis View Post
    Thanks for your reply.

    The longest tempauthor would receive for the test data is 15 characters. I changed it to reflect this:

    Code:
    	char * tempauthor = new char;
    	cout << "Enter author: ";
    	cin.getline(tempauthor, 16); 
    	return tempauthor;
    Unless I misunderstood your point and it needs to match what was declared in the 3dim array it's entering into (in this case, 50). I tried it both ways and it still crashes at the same spot (with the same test data).
    You missed the point. How many characters are you allocating for tempauthor?

    And seriously, if I were your instructor I'd have you rewrite it without 3d arrays. That's just not how to tackle this problem.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Seemingly Random Crashes @ array input

    I guess this must be the "convince them what *not* to do by experience" school of teaching....

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    Quote Originally Posted by ayjis View Post
    @GCDEF, Lindley:
    My professor requires it this way. In order to teach us pointers, we make every array a pointer. This assignment also specifies we use 3-dim arrays.
    I say this very sincerely. Drop the class and look for a teacher with a clue. First, this is seriously an abhorrent way to code. If somebody that worked for me wrote that, they'd have to rewrite it or look for another job. If you took it to a job interview, it would be a very short interview. Also, the error in your code is very easy to spot. The fact that your instructor missed it doesn't bode well either. I mean it. You're being seriously led astray. What school is this? Can you paste the text of the actual assignment in here?

    On the bright side, kudos for your first post having code tags and a good, easy to read indentation style.
    Last edited by GCDEF; April 21st, 2010 at 07:42 PM.

  10. #10
    Join Date
    Apr 2010
    Posts
    5

    Re: Seemingly Random Crashes @ array input

    I see what you mean now about allocating. I'll get to it and post back with any other questions.

    Here is the entire assignment:
    Code:
    CPSC 121 Assignment 4 
    Books 
    
    Introduction 
    
    This course is all about arrays.  Here is another problem to solve that uses arrays.  We'll continue practicing the use of arrays through the end of the semester.  This program has one new kind of array -- a three dimensional array. 
    
    
    Problem description 
    
    Suppose you work for Barnes and Noble.  A manager asks you to make software to track the inventory of books.  Make a C++ program that will do the job of managing the data about these books. 
    
    The data relevant to a single book are these: isbn, author, publisher, title, edition number, year, number of pages, manufacturer's retail price, b&n discounted price, and number of copies in stock. 
    
    Create arrays to hold the data.  You may assume that the total number of books will never exceed 25 for this simple homework. 
    
    
    Activities requested by the management
    
    Display all the books in a orderly readable form. 
    
    Add a single book to the inventory. 
    
    Find the oldest book in the inventory
    
    Find the cheapest book according to discounted prices
    
    Display only books published in a given year 
    
    Sort books by author:  then books will display in order by author name 
    
    Find the books with number of pages exceeding a certain minimum.
    
    //Find all the information about a book given its title = not used in this assignment.
    
    
    Arrays 
    
    It is important to make the right foundation before programming.  After too much program has been written with the wrong foundation it is too hard to go back and start a new design.  First collect together data of the same type into arrays.  That means integer data will be in an integer array, float data in a float array, and so on.  Be sure to come to an understanding of the problem to the point that you know the purpose of every piece of data.
    
    
    Functions
    
    Here is a list of functions to be included in your program.  Your program must have these functions, but you may create more functions if that helps.
    
    1.  A function that reads all the data regarding one book and places those data into their respective arrays.  The prototype probably looks like this.
    void readonebook(char *** info, int ** data, float ** costs, int numberbooksininventory);
    
    2.  A group of function that will be called by #1 above.  Here are the prototypes.
    char * readauthor();
    char * readtitle();
    char * readpublisher();
    float readmsrp();
    float readbandn();
    int readisbn();
    and others of your choice.
    
    3.  A function that will show all books in the inventory.  The prototype looks like this.
    void showallbooks(char *** info, int ** data, float ** prices, int numberbooksatbarnesandnoble);
    
    4.  A function that will output only one book.  The prototype looks like this.
    void displayonebook(char * title, char * publisher, char * author, int isbn, int edition, int year, int pages, int copies, float msrp, float discountprice);
    
    5.  A function that will find the index number of the oldest book in the inventory.
    int findoldest(int ** bookdata, int numberofbooksintheinventoryatbarnesandnoble);
    
    6.  A function that will find the index number of the cheapest book sold by Barnes and Noble according to discount prices.
    int findcheapestbookintheinventory(float ** bookprices, int totalbooksatbandn);
     
    7.  A function that will show all the books published in a certain year.  These prototypes are lots of fun.
    void showoneyearofbooks(char *** information, int ** numbericdata, float ** dollars, int totalcountofallbooks, int specialyear);
    
    8.  A function that will show all the big books; that means, all books with more that a certain minimum number of pages.  This is more fun than any programmer should have:
    void showallbigbooks(char *** textinformation, int ** numberbookinformation, int ** costs, int grandtotalnumberinventory, int minimumpagecount);
    
    9.  A function that will create the 3-dim array.  This function does not have to store any useful information in the array – it simply has to reserve space for the array.  Main will call this function to establish the existence of the array.
    char *** createtextarray();
    In CPSC131 functions that create objects (like array) are especially important.  Such function are designated as “constructor functions”.
    
    10.  A function that will create the array of prices.  Main will call this function.  This is one of those constructor functions.   float ** createprices();
    
    
    Test data
    
    Display all the data in organized format.  //Here at the beginning there are no data.
    
    Add this book to the inventory
    0132084945
    Daniel Liang
    Introduction to Java
    Prentice-Hall
    7
    2009
    1297
    89.00
    78.00
    30
    
    Add this book to the inventory
    1590599918
    Keir Thomas
    Beginning Ubuntu Linux
    Apress
    3
    2008
    729
    59.99
    50.00
    10
    
    Add this book to the inventory
    1861004060
    Paul Wilton
    Beginning Javascript
    Wrok
    1
    2000
    1018
    62.50
    55.00
    7
    
    Find oldest book in the inventory
    
    Add this book to the inventory
    0127386718
    Peter Wayner
    Disappearing Cryptography
    Academic Press
    1
    1996
    295
    33.95
    25.50
    4
    
    Add this book to the inventory
    0764539108
    Danny Briere
    Wireless Home Networking for Dummies
    Wiley Publishing
    1
    2003
    362
    25.75
    20.25
    11
    
    Add this book to the inventory
    0767908155
    Mario Livio
    The Golden Ratio
    Broadway Books
    1
    2002
    294
    23.95
    21.60
    23
    
    Add this book to the inventory
    0691118221
    Paul J. Nahin
    Dr. Euler's Fabulous Formula
    Princeton University Press
    1
    2006
    380
    24.99
    19.00
    17
    
    Display all the books' data in organized format
    
    Find the cheapest book in B&N inventory; display its information
    
    Find all books with more than 400 pages; display all their information.
    
    Sort the books according to author's (first) name
    
    Display all the books in the inventory
    
    Add this book
    0691099839
    Julian Havil
    Gamma: Exploring Euler's Constant
    Princeton University Press
    1
    2003
    266
    24.99
    20.00
    17
    
    Find all books with more than 270 pages; display all their information.
    
    Show the information about all books published in 2003
    
    Find oldest book in the inventory
    
    Show the information about all books published in 2001
    
    Add this book
    0375424045
    Leonard Mlodinow
    The Drunkard's Walk
    Pantheon Books
    1
    2008
    252
    27.45
    22.10
    29
    
    Add this book
    0136053068
    Paul Deitel
    Java How to Program
    Prentice Hall
    8
    2009
    1506
    85.99
    65.00
    40
    
    0470497025
    Jeff Duntemann
    Assembly Language Step by Step
    Wiley and Sons
    3
    2009
    648
    65.00
    40.95
    12
    
    0122407601
    Richard Epstein
    The Theory of Gambling and Statistical Logic
    Academic Press
    2
    1977
    464
    45.00
    24.95
    4
     
    Show the information about all books published in 2008
    
    Sort the inventory by author's name
    
    Find all books with more than 450 pages; display all their information.
    
    Find and show the cheapest book in the inventory
    
    Find and show the oldest book in the inventory
    
    Display all the books in the inventory
    
    Quit the program

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    That's a reasonable assignment except for the three dimensional part. A single dimensional array is all you need. I'm having trouble imagining what he's even thinking.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Seemingly Random Crashes @ array input

    It is important to make the right foundation before programming. After too much program has been written with the wrong foundation it is too hard to go back and start a new design. First collect together data of the same type into arrays. That means integer data will be in an integer array, float data in a float array, and so on. Be sure to come to an understanding of the problem to the point that you know the purpose of every piece of data.
    Funny how it starts out by saying exactly the right thing, and then goes on to say exactly the wrong thing. Parallel arrays are to be avoided in favor of more cohesive data structures. Data should be grouped by relevance, not just by data type.

    The one benefit I can see to making you do it this way is that if you can keep track of all that indirection, you'll have pointers down pat. Plus it'll make you appreciate the right approach all the more when you see it.

  13. #13
    Join Date
    Apr 2010
    Posts
    5

    Re: Seemingly Random Crashes @ array input

    @GCDEF

    Thank you very much for your help! Everything seems to be working fine now.

    I ended up just passing the array to the functions and entering directly into the array, skipping the middleman for the char functions. I left the float/int ones alone since they worked properly. Did I go about this the right way? I can't rely on my teacher to comment on this, it would seem.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: Seemingly Random Crashes @ array input

    Quote Originally Posted by ayjis View Post
    @GCDEF

    Thank you very much for your help! Everything seems to be working fine now.

    I ended up just passing the array to the functions and entering directly into the array, skipping the middleman for the char functions. I left the float/int ones alone since they worked properly. Did I go about this the right way? I can't rely on my teacher to comment on this, it would seem.
    I'm glad it works, but from a design standpoint, no it's not right.

    Ideally, assuming you aren't using a database, you'd want a class or struct that held information about the books, and store those structs in an array or list. I haven't tried to figure out how you're using 3D arrays, but you'd really have to go out of your way to come up with a really convoluted design to make them fit.

    Unfortunately, in school, if it's what your instructor wants, it's right. Keep in mind it's a learning exercise I guess. In the real world, code won't look like that.

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Seemingly Random Crashes @ array input

    I thought you might appreciate seeing how this sort of task should be implemented. Or at least a better approach---there are of course many ways it could be done. Files are attached, plus the code is shown here.

    Given your assignment requirements you cannot simply use the attached, of course (which is why I did it----we don't do homework), but it does all the same things. The biggest difference from the assigned tasks is that it loads up all the books at once instead of doing some of the searches before they're all in. Given that decision, I omitted some of the outputs which would have been duplicates.

    This took about 45 minutes to write. And guess how much debugging it took to get this working correctly? After I finished writing it, I found I had 4 compile errors. And once I fixed those, everything worked the first time I ran it. There is no way in hell anyone writing a program involving a dozen triple-pointers would be able to make such a claim.

    If you think all of those very-similar comparator functors are a bit of a pain, you're right---that's why C++1x is introducing lambda functions, which are a sort of notation for writing this sort of functionality even more compactly.

    Code:
    /*
     *  inventory.cpp
     *  
     *
     *  Created by Lindley French on 4/21/10.
     *  Copyright 2010 __MyCompanyName__. All rights reserved.
     *
     */
    
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <functional>
    
    using namespace std;
    
    struct Book
    {
    	string name;
    	string author;
    	string publisher;
    	int ISBN;
    	int edition;
    	int year;
    	int pages;
    	double MSRP;
    	double BNprice;
    	int inStock;
    };
    
    struct YearComparator
    {
        bool operator()(const Book &b1, const Book &b2) const
    	{
    	    return b1.year < b2.year;	
    	}
    };
    
    struct AuthorComparator
    {
        bool operator()(const Book &b1, const Book &b2) const
    	{
    	    return b1.author < b2.author;	
    	}
    };
    
    struct DiscountedPriceComparator
    {
    	bool operator()(const Book &b1, const Book &b2) const
    	{
    	    return b1.BNprice < b2.BNprice;	
    	}
    };
    
    struct BigBookCondition: unary_function<Book, bool>
    {
        int minsize;
    	BigBookCondition(int minsize_): minsize(minsize_)
    	{}
    	bool operator()(const Book &b) const
    	{
    		return b.pages >= minsize;
    	}
    };
    
    struct YearCondition: unary_function<Book, bool>
    {
    	int targetyear;
    	YearCondition(int targetyear_): targetyear(targetyear_)
    	{}
    	bool operator()(const Book &b) const
    	{
    		return b.year == targetyear;
    	}
    };
    
    istream& operator>> (istream &in, Book &b)
    {
    	in >> b.ISBN;
    	in.ignore(100,'\n');
    	getline(in, b.author);
    	getline(in, b.name);
    	getline(in, b.publisher);
    	in >> b.edition;
    	in >> b.year;
    	in >> b.pages;
    	in >> b.MSRP;
    	in >> b.BNprice;
    	in >> b.inStock;
    	return in;
    }
    
    // Output could be labelled and made more fancy-looking, but it's a good idea to make the input and output methods use the same format.
    ostream& operator<< (ostream &out, const Book &b)
    {
    	out << b.ISBN << "\n";
    	out << b.author << "\n";
    	out << b.name << "\n";
    	out << b.publisher << "\n";
    	out << b.edition << "\n";
    	out << b.year << "\n";
    	out << b.pages << "\n";
    	out << fixed << setprecision(2) << b.MSRP << "\n";
    	out << fixed << setprecision(2) << b.BNprice << "\n";
    	out << b.inStock;
    	return out;
    }
    
    void readInventory(vector<Book> &inventory, const string &filename)
    {
    	ifstream in(filename.c_str());
    	Book tmp;
    	
    	while (in >> tmp)
    		inventory.push_back(tmp);
    }
    
    void listInventory(const vector<Book> &inventory)
    {
        for (unsigned i = 0; i < inventory.size(); i++)
    		cout << inventory[i] << endl << endl;
    }
    
    int findOldest(const vector<Book> &inventory)
    {
    	return distance(inventory.begin(),
    					min_element(inventory.begin(),inventory.end(),YearComparator()));
    }
    
    int findCheapestDiscounted(const vector<Book> &inventory)
    {
    	return distance(inventory.begin(),
    					min_element(inventory.begin(),inventory.end(),DiscountedPriceComparator()));
    }
    
    int sortByAuthor(vector<Book> &inventory)
    {
    	sort(inventory.begin(),inventory.end(),AuthorComparator());
    }
    
    int listBooksFromYear(const vector<Book> &inventory, int year)
    {
    	vector<Book> yearbooks;
    	remove_copy_if(inventory.begin(),inventory.end(),back_inserter(yearbooks),not1(YearCondition(year)));
    	cout << "Books from " << year << ":\n";
    	listInventory(yearbooks);
    }
    
    int listBooksOverSize(const vector<Book> &inventory, int minpages)
    {
    	vector<Book> bigbooks;
    	remove_copy_if(inventory.begin(),inventory.end(),back_inserter(bigbooks),not1(BigBookCondition(minpages)));
    	cout << "Books over " << minpages << " pages:\n";
    	listInventory(bigbooks);
    }
    
    int main(int argc, char **argv)
    {
        if (argc < 2)
    		printf("Usage: Inventory <bookfile>\n");
    	
    	vector<Book> inventory;
    	readInventory(inventory, argv[1]);
    	
    	cout << "Oldest book:\n" << inventory[findOldest(inventory)] << endl;
    	cout << "Cheapest book:\n" << inventory[findCheapestDiscounted(inventory)] << endl;
    	listBooksOverSize(inventory, 400);
    	sortByAuthor(inventory);
    	cout << "Complete inventory sorted by author:\n";
    	listInventory(inventory);
    	listBooksOverSize(inventory, 270);
    	listBooksFromYear(inventory, 2003);
    	listBooksFromYear(inventory, 2001);
    	listBooksFromYear(inventory, 2008);
    	listBooksOverSize(inventory, 450);
    	
    	return 0;
    }
    EDIT: Okay, apparently I need to go switch off tabs in XCode....it was all much more aligned on my end.
    Attached Files Attached Files
    Last edited by Lindley; April 21st, 2010 at 09:09 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center