Whats wrong with my file streaming[it compiles fine]

This is a discussion on Whats wrong with my file streaming[it compiles fine] within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> // The basic C++ header file #include <fstream> // The header file used to make output and ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    Whats wrong with my file streaming[it compiles fine]

    Code:
    #include <iostream>         // The basic C++ header file
    #include <fstream>         // The header file used to make output and input files
    #include <conio.c>
    #include <string>         // for strings[group of words/text]
    #include <windows.h>       // For some basic window function obviously
    #define done 0
    
    typedef void function;           // not necessary, just the way I program
    typedef unsigned short int Zint;  // Also not necessary, Zint stands for ZOO Integer
    
    class database              // Our first class
    {
     public:
     function GetInfo();
     function LoadInfo(ifstream &fin);
     function DCS(string sz, Zint x, Zint y, WORD color);    // DCS stands for Draw Color String
    
     private:
     char *name;
     Zint *age;
     string eater;    // strange, I know
     ofstream fout;
    
    }obf;
    
    function database::GetInfo()
    {
     name= new char[30];
     age= new Zint[10];
    
     cout << "Input your fullname: ";
     cin.getline(name, 30, '\n');
     cout << "\nInput your age: ";
      cin >> age[0];
    
     fout.open("info.txt");
     fout << "Name: " << name;
     fout << "\nAge: " << age[0];
     fout.close();
    
     delete [] name;
     delete [] age;
    
    }
    
    function database::LoadInfo(ifstream &fin)
    {
     fin.open("info.txt");
      if (fin.fail() ) {
      cout << "\nSorry, unable to find \"info.txt\"....." << endl; }
     while (fin >> eater ) {
      if (!fin.eof() ) {
      cout << eater;
      fin.clear(); }
      else {
      fin.close(); }
     }
    
     fin.close();
    }
    
    function database::DCS(string sz, Zint x, Zint y, WORD color)
    {
     HANDLE output= GetStdHandle(STD_OUTPUT_HANDLE);
     COORD cursor={x, y};
    
     SetConsoleCursorPosition(output, cursor);
     SetConsoleTextAttribute(output, color);
    
     cout << sz;
    
    }
    
    int main(Zint argc, char *argv[])
    {
     ifstream some;
     Zint *numbers; numbers=new Zint[10];
     database *zoo;
     zoo= new database;
    
     zoo->DCS("********************************", 20, 5, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("****    The Main Menu!!     ****", 20, 6, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("****                        ****", 20, 7, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("****  1. Print Info         ****", 20, 8, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("****  2. Load Info          ****", 20, 9, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("****                        ****", 20, 10, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("********************************", 20, 11, FOREGROUND_RED | FOREGROUND_INTENSITY);
     zoo->DCS("What is Thy Desire? ", 24, 13, FOREGROUND_RED | FOREGROUND_INTENSITY);
      cin >> numbers[0];
    
     switch(numbers[0]) {
      case 1: clrscr();
      zoo->GetInfo();
            break;
    
      case 2: clrscr();
      zoo->LoadInfo(some);
            break;
      default:
            break;
     }
    
     delete zoo;
     cin.get();
     return done;
    }
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I found two problems that effected the output after a quick glance. The first was in your GetInfo method, there was something still in the input stream when this method was called, so the call to getline read just a newline and continued. I fixed it by adding

    cin.ignore();

    Just before the call to getline in GetInfo. The second problem was in the LoadInfo method. You were using the >> operator of fin when getline would have been more appropriate based on the file formatting. I made a few quick changes, here they are:
    Code:
    function database::LoadInfo(ifstream &fin)
    {
      fin.open("info.txt");
      if ( fin.is_open() ) {
        while ( fin.good() ) {
          getline ( fin, eater );
          cout<< eater <<endl;
        }
        fin.close();
      }
      else
        cerr<<"\nSorry, unable to find \"info.txt\".....\n";
    }
    After those changes the program works perfectly, but I didn't check it more thoroughly for hidden bugs. So be aware that they may exist.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    where to add cin.ignore(); ?
    after the getline or what?
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >where to add cin.ignore(); ?
    Code:
    function database::GetInfo()
    {
     name= new char[30];
     age= new Zint[10];
    
     cin.ignore(); // Add it here
     cout << "Input your fullname: ";
     cin.getline(name, 30, '\n');
     cout << "\nInput your age: ";
      cin >> age[0];
    
     fout.open("info.txt");
     fout << "Name: " << name;
     fout << "\nAge: " << age[0];
     fout.close();
    
     delete [] name;
     delete [] age;
    }
    You want cin.ignore() to clear up the input stream for you *before* attempting anymore reads or you will still read the garbage.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21