Hey, I have a homework question that requires me to create a program that takes in a file containing information on some books. The file has a title, author, publisher, and isbn number
for example:
A Book on CAl Kelly and Ira Pohl
Addison-Wesley, Fifth Edition 1998.
0201183994
C How to Program
Paul Deitel and Harvey Deitel
Prentice Hall Sixth Edition 2010
0136123562
First thing's first, I tried to make a while and for loop to go over every line and store the information in respective strings. Problem is, that for some reason, when it gets to one of the lines, the program outputs a zero. I'm not sure why.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "Header.h"
using namespace std;
int main(){
ifstream inFS; // The input stream for the log file
string hit; // Stores a line from the file
string filename; // Stores the filename we'll open
const int TABLE_SIZE = 200;
// Ask the user for a filename, and open it up
cout << "Print the name of the file you wish to open" << endl << endl;
//cin >> filename;
filename = "books.txt";
inFS.open(filename.c_str());
if (!inFS.is_open()) {
cout << "Could not open file " << filename << ".txt" << endl;
return 1; // 1 indicates error
}
// Loop over every line in the file
while (!inFS.eof()) {
// Store one line in 'hit'
for(int i = 0; i<4;i++){
if(i = 0){
getline (inFS, hit);
string title = hit;
cout << title << endl;
}
if(i = 1){
getline (inFS, hit);
string author = hit;
cout << author<< endl;
}
if(i = 2){
getline (inFS, hit);
string publisher = hit;
cout << publisher << endl;
}
if(i = 3){
getline (inFS, hit);
int isbn;
cout << isbn << endl;
}
if(i = 4){
getline (inFS, hit);
string dummy = hit;
cout << dummy << endl;
}
}
}
return 0;
}