Book shop Program
understood, Ijust did it.
You still still not reading from the file correctly. You have your input file formatted in a certain way, so you must read it in a certain way.
Please consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("books.txt");
if(!infile) {
cerr << "Unable to open file.\n";
exit(1);
}
string parse; // collecting substr from this string
while(getline(infile, parse)) {
// Find the delim positions
size_t delimPos[5]; // there are 5 semicolons per line
delimPos[0] = parse.find(';');
for(int i = 1; i < 5; i++) {
delimPos[i] = parse.find(';', delimPos[i-1]+1);
}
string bookID, author, bookTitle, isbn, price, quantity;
bookID = parse.substr(0, delimPos[0]);
author = parse.substr(delimPos[0]+1, delimPos[1]-delimPos[0]-1);
bookTitle = parse.substr(delimPos[1]+2, delimPos[2]-delimPos[1]-2);
isbn = parse.substr(delimPos[2]+2, delimPos[3]-delimPos[2]-2);
price = parse.substr(delimPos[3]+2, delimPos[4]-delimPos[3]-2);
quantity = parse.substr(delimPos[4]+2);
}
return 0;
}
|
Topic archived. No new replies allowed.