Thread: Okay its running with a serious glitch

  1. #1
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373

    Okay its running with a serious glitch

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    #include <iomanip>
    #include <windows.h>
    using namespace std;
    inline void eatline() { while (cin.get() != '\n') continue; }
    struct dog
    {
    char name[20];    //name
    double weight;    //weight
    char b[20];         //breed
    };
    
    const char * file = "dogs.dat";
    int main ()
    {
        dog d1;
        cout << fixed << right;
    
    
    //show initial contents
        ifstream fin;
        fin.open(file, ios::in |ios::binary);
    //NOTE: Some Systems don't allow ios::binary
        if (fin.is_open())
        {
        cout << "Here are the current contents of the " << file << " file:\n";
        while (fin.read((char *) &d1, sizeof d1))
              {
              cout << setw(20) << d1.name << ": "
                   << setprecision(0) << setw(12) << d1.weight
                   << setprecision(2) << setw(6) << d1.b << "\n";
              }
    }
    fin.close();
    
    //add new data
    ofstream fout(file, ios::out | ios::app | ios::binary);
    //Note: Some systems don't allow ios::binary
    if (!fout.is_open())
    {
        cerr << "Can't open " << file << " file for output:\n";
        exit(1);
    }
    
    cout << "Enter dogs name (enter blank line to quit): \n";
    cin.get(d1.name, 20);
    while (d1.name[0] != '\0')
    {
          eatline();
          cout << "Enter dogs weight: ";
          cin >> d1.weight;
          cout << "Enter breed: ";
          cin >> d1.b;
          eatline();
          fout.write((char *) &d1, sizeof d1);
          cout << "Enter dogs name (Blank line to quit):\n";
          cin.get(d1.name, 20);
    }
    fout.close();
    
    //show revised
           fin.clear();
           fin.open("dogs.dat", ios::in | ios::binary);
           if (fin.is_open())
           {
           cout << "Here is the new contents of the " << file << "file:\n";
           while (fin.read((char *) &d1, sizeof d1))
                 {
                 cout << setw(20) << d1.name << ": "
                      << setprecision(0) << setw(12) << d1.weight
                      << setprecision(2) << setw(6) << d1.b << "\n";
                      }
                 }
                 fin.close();
                 cout << "Done.\n";
                return 0;
    }
    Please compile and run a few times to see whats wrong
    This war, like the next war, is a war to end war.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    > cout << fixed << right;

    not sure what thats supposed to be, but those variables havent been defined anywhere.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    Linux Forever
    Join Date
    Oct 2002
    Posts
    373
    Working fine now..... I was using a bad variable d1, i needed dl.
    This war, like the next war, is a war to end war.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  3. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  4. Replies: 2
    Last Post: 05-12-2006, 10:28 AM
  5. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM