Thread: Why O Why dosen't this work?

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    5

    Why O Why dosen't this work?

    Hello,

    I have having some trouble with my code not working.
    In my book I am in the file i/o part so this is what the exercise is on.
    My program is one where the user can:
    1) make a record in the categories model, make, year, and mileage,
    2) read a record for those same categories
    3)exit

    I have two different functions for the writing and the reading.
    My problem is that when the user enters to write or read a file the program immediately skips the line that allows the user to type in a file and skips to "the file could not be open" My only clue to what the problem might be is that I use getline and cin in the same program, which I have heard can cause problems.

    Your help would be much appreciated,
    styles

    Below is the code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int get_int(int default_value);
    void write_bin();
    void read_bin();
    
    char model[21];
    char make[21];
    char year [6];
    
    int main() {
        while (1) {
            int choice;
            cout << "Welcome to the car file system" << endl;
            cout << "******************************" << endl;
            cout << "Press 1 to write a record." << endl;
            cout << "Press 2 to read a record." << endl;
            cout << "Press 3 to exit." << endl;
            cin >> choice;
            if (choice == 1) {
                    write_bin();
            }
            if (choice == 2) {
                    read_bin();
            }
            if (choice == 3){
                    system("PAUSE");
                    return 0;
            }
        }
    }
    
    void write_bin() {
        char filename[81];
        int n;
        int mileage;
        int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
        
    
        cout << "Enter file name: ";
        cin.getline(filename, 80);
    
        // Open file for binary read and write.    
    
        fstream  fbin(filename, ios::binary | ios::in | ios::out);
        if (!fbin) {
            cout << "Could not open file " << filename;
        }
    
    //  Get record number to write to.
        while (1) {
            cout << "Enter file record number: ";
            n = get_int(0);
            if (n == -1) {
                    break;
            }
    
        // Get data from end user.
    
            cout << "Enter model: ";
            cin.getline(model, 20);
            cout << "Enter make: ";
            cin.getline(make, 20);
            cout << "Enter year: ";
            cin.getline(year, 5);
            cout << "Enter mileage: ";
            mileage = get_int(0);
    
        // Write data to the file.
     
            fbin.seekp(n * recsize);
            fbin.write(model, 20);
            fbin.write(make, 20);
            fbin.write(year, 5);
            fbin.write(reinterpret_cast<char*>(&mileage), sizeof(int));
            fbin.close();
         }
         
    }
    
    void read_bin() {
        char filename[81];
        int n;
        int mileage;
        int recsize =  sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
        
    
        cout << "Enter file name: ";
        cin.getline(filename, 80);
    
        // Open file for binary read-write access.
    
        fstream  fbin(filename, ios::binary | ios::in | ios::out);
        if (!fbin) {
            cout << "Could not open file " << filename;
        }
    
    	while(1) {
    
            // Get record number and go to record.
    
            cout << "Enter file record number (-1 to quit): ";
            n = get_int(-1);
    		if (n == -1)
    		    break;
            
    		fbin.seekp(n * recsize);
    
            // Read data from the file.
    
            fbin.read(model, 20);
            fbin.read(make, 20);
            fbin.read(year, 5);
            fbin.read(reinterpret_cast<char*>(&mileage), sizeof(int));
    
            // Display the data and close.
        
            cout << "The model is: " << model << endl;
            cout << "The make is: " << make << endl;
            cout << "The year is: " << year << endl;
            cout << "The mileage is: " << mileage << endl;
        }
        fbin.close();
      
    }
    
    int get_int(int default_value) {
        char s[81];
    
        cin.getline(s, 80);
        if (strlen(s) == 0)
             return default_value;
        return atoi(s);
    }
    Last edited by styles; 03-27-2007 at 08:15 PM. Reason: Added info

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem is that when the user types the answer to the first question, they type a number and hit <enter>. The newline character that comes from that <enter> is left in the input stream after you read in the number. Later, you call getline, which is designed to read everything until it finds a newline. Since there is still a newline from the previous input, it stops immediately.

    The solution is to add cin.ignore() after your calls to cin >>, especially if you know they come right before a call to getline. (You can safely put cin.ignore() after all calls to cin >> since you will be ignoring whitespace anyway).

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Thanks, Daved

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    I have come across another problem...
    I added cin.ignore() after cin << choice.
    The program lets me write different information in different records, however, when I try to view the records, all the records read the same as the data I put in for record 0.
    What's going on?
    Thanks for your time,

    styles

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    Well I solved my problem.
    In the write_bin function it should have passed -1 not 0 to the get_int function.
    And the loop in the write_bin function should have included the whole function to recalculate the record size each time and possibly the part that opens the file for binary input and output (although I am not sure).

    Anyway it works now.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    5
    After trying different snippets of code I realize that the problem wasn't solved for any of those reasons. The problem was that I closed the file, and then I didn't reopen it in the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. Why dosen't this work
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 08-16-2002, 08:18 PM
  5. sigh... why dosent this work?
    By jon_nc17 in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 08:30 PM