Long story short, I am a Programming II major. We are compiling and writing these programs using an SSH client...
We have to retrieve data from a data file, I have the second data file working, but the first one has two separate values for author in the same line, and we have to extract them, store them into an array, the manipulate that data later. Would anyone happen to know why I keep getting a "Segmentation Error". I know the error is inside the inner while loop, but I don't know where it is going out of bounds in the array...any help is much appreciated. The area I'm having a problem with is in the first nested while loop....
Thank you for any help!
#include "bookType.h" //This incorporates the bookType.h file,
#include "memberType.h" //Same thing
#include <iostream> //These are all declarations
#include <iomanip> //More declarations...etc
#include <string>
#include <fstream>
using namespace std;
const int BKNUM = 1000; //This is the prespecified size of the bookType book array
const int MBRNUM = 100; //This is the prespecified size of the memberType member array
int main()
{
bookType book[BKNUM]; //This initializes the array values to zero, I believe.
memberType member[MBRNUM]; //same
char choice; //This is for use in the switch menu, it allows the user to navigate using a series of single digits
int i = 0; //This is used to control the loops that read into and write into the array
int m = 0;
int n = 0;
int j = 0;
ifstream infile; //Infile!!!!
string IDnum;
string title, authors[10], publisher, ISBN, ID, firstName, lastName, author2; //String variable declarations
int noOfAuthors, year, quantityInStock, quantitySold, booksPurchasedRecently, booksPurchased; //Int variable declarations
double price, amountSpentRecently, amountSpent; //Double variable declarations
while(end >= 0){ // THIS IS THE AREA WITH THE ERROR!!!!!!!
authors[x] = author2.substr(start, end - start);
start = end + 1;
author2 = author2.substr(start, author2.length() - start);
++x;
end = author2.find(";");
start = 0;
}
getline(infile, publisher); //After this part, it should be smooth sailing to read the data objects into the array.
infile >> year; //More of the Same.
getline(infile, ISBN);
infile >> price;
infile >> quantityInStock;
infile >> quantitySold;
book[m].setBookInfo(title, noOfAuthors, authors, publisher, year, ISBN, price, quantityInStock, quantitySold);
++m;
}
infile.close();
*/
//And now this will open the member file and read it into the array.
infile.open("member.dat");
infile.close(); //This should close the member.dat file after it has been read into array.
cout << endl << endl;
cout << "To make a selection, enter the number and press ENTER." << endl; //Ask if I need to have this here or if the while loop will simply
cout << "1: Book/ISBN list" << endl; //reload the menu list after each option is selected.
cout << "3: Member ID/name list" << endl;
cout << "4: Member information" << endl;
cout << "5: Book Purchase" << endl;
cout << "6: Exit program" << endl << endl;
cout << "Option: ";
cin >> choice;
cout << endl << endl;
while(choice != '6'){
switch(choice)
{
case '1':
// cout << "Book/ISBN list" << endl;
// for(int i = 0; i < BKNUM; ++i)
// cout << book[i].getTitle() << ' ' << book[i].getISBN() << endl; //This will read out the book title list and ISBN of each book if I
//can get it to work. Might need to add setprecision with iomanip later.
cin >> choice;
//code here
//code here too
break;
case '2': //Will work on all of this after I get Array and File input correct.
cout << "Book Information" << endl;
cout << "This part of the program doesn't work, because I was unable to work around the segmentation error." << endl;
cin >> choice;
//code here
//code here, too
break;
case '5':
cout << "Book Purchase" << endl;
cin >> choice;
//code here
//code here, too
break;
default:
cout << "The choice was invalid. Please try again or press '6' to exit." << endl;
break;
}
//This is what I refer to as my 'infinite loop stopper'.
2. If you are a programming major, you had better learn how to use your debugger - in a simple program like this, the debugger will tell you what your error is, faster than you can type up a post to this forum.