Thread: Reading a file help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    Reading a file help

    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;
    }
    Reading a file help-problem-png
    Last edited by Langard; 05-06-2013 at 05:50 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look closely at the following snippet:
    Code:
           for(int i = 0; i<4;i++){
                if(i = 0){
    Do you know that the operator= is the assignment operator? Comparison is done with the comparison operator==.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  2. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  3. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  4. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM

Tags for this Thread