Author Topic: ...how about <fstream>  (Read 6585 times)

Yap Michael

  • User
  • *
  • Posts: 8
...how about <fstream>
« on: February 18, 2006, 08:37:35 AM »
Thanks for the link! I learned new things about constructors and also destructors.  Please check the codes i created below. I don't know how to include all the characters inside the notepad. Is there something wrong about it? Please add some codes to make it clear for me.

Thanks!

<B>#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

struct employees
{
     char name[90];
     int hours;
     void input();
     void inputHours();
     void display();
};

int main ()
{
    ifstream fin;
    char answer;
    do
    {
     ofstream fout;
     fout.open("user.txt");
     
     employees emp;
     cout << "Enter your name: \n" << endl;
     fout << "Enter your name: " << endl;
     emp.input();
     fout.close();
     
     fout.open("user.txt", ios::app);
     fout << "\nEnter hours worked: " << endl;
     cout << "\nEnter hours worked: \n" << endl;
     emp.inputHours();
     fout.close();
     
     emp.display();
     
     cout << "If you want to see the saved file, press n" << endl;
     cout << "Press y to retry" << endl;
     cin >> answer;
     }
     while (answer == 'y' || answer == 'Y');
     
     string s1, s2, s3, s4, s5, s6, s7;
     
     fin.open("user.txt");
     if (fin.fail () )
    {
                 cout << "File failed!" << endl;
                 system("PAUSE");
                 exit (1);
                 }
     fin >> s1, s2, s3, s4, s5, s6, s7;
     
     cout << s1 << s2 << s3;
     cout << s4
          << s5 << s6
          << s7 << endl;
     fin.close();
     
     system("PAUSE");
     return 0;
     }
     
void employees::input()
{
     ofstream fout;
     fout.open("user.txt", ios::app);
     cin >> name;
     fout << name;
}
void employees::inputHours()
{
     ofstream fout;
     fout.open("user.txt", ios::app);
     cin >> hours;
     fout << hours << endl;
     fout.close();  
}
void employees::display()
{
    cout << endl;
    cout << name << " has worked " << hours << " hours " << endl;
}<B/>

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
Must test code first
« Reply #1 on: February 18, 2006, 12:22:23 PM »
You seem to be confused about structs and classes.

Code: [Select]

struct employees
{
     char name[90];
     int hours;
     void input();
     void inputHours();
     void display();
};


Your definition is using a struct as if it is a class. I dont know if you did it on purpose, or you were simply confused because you seemed to be mixed up about structs and classes.

I had doubts whether your program would compile but I tested it anyway.  It did!  That seemed odd to me so I had to check the book then the Internet.

I found this http://carcino.gen.nz/tech/cpp/struct_vs_class.php very informative.  

Like most typical guys, I started out with C and later moved on to C++.  This post made me realize that our textbook does contain a glaring error on the difference between structs and classes.  Like most others, I simply assumed that C structs were the same as C++ structs.  It is a terrible mistake for me to be teaching this misconceptions!

But anyway, I still prefer that you define this as a class rather than as a struct.  

Quote
in C++, a struct is identical to a class except that the members of a struct have public visibility by default, but the members of a class have private visibility by default


In most cases we would expect member values to be private by default and let accessor & mutator methods handle I/O.  Since you allowed the member value employee::hours to be public, it is pointless to define accessor/mutator for it.
Analyze. Design. Develop. Debug. Deploy. Maintain.

louiecerv

  • Professor
  • User
  • *
  • Posts: 85
better to use a loop
« Reply #2 on: February 18, 2006, 12:38:39 PM »
Code: [Select]

string s1, s2, s3, s4, s5, s6, s7;

fin.open("user.txt");
if (fin.fail () )
{
cout << "File failed!" << endl;
system("PAUSE");
exit (1);
}

fin >> s1, s2, s3, s4, s5, s6, s7;

cout << s1 << s2 << s3;
cout << s4
<< s5 << s6
<< s7 << endl;
fin.close();



Shouldn't you be using a loop to do this?  There were 3 items to read and you used 7 strings  to retrieve them.

This would do a better job:
Code: [Select]

string next;
while (fin >> next)
{
       cout << next;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
...how about <fstream>
« Reply #3 on: February 18, 2006, 03:13:42 PM »
It is certainly true that structs differ from classes only in default visiblity.  Despite that, I have been known to use struct/class to assign a stronger meaning to an object.  I use structs rarely in C++, but when I do I use the struct to mean that it is an "open object" i.e. not utilizing encapsulation.  My personal rule is that a struct should not have any more methods besides a constructor and possibly a destructor, otherwise I follow encapsulation and make it a fully-designed class.  Typically I only use struct to store related pieces of data for a short time, such as loading rectangle coordinates from a file or something.
Gillius
Gillius's Programming http://www.gillius.org/

Yap Michael

  • User
  • *
  • Posts: 8
thanks...
« Reply #4 on: February 19, 2006, 01:21:45 AM »
Thanks for the help! There's so much things we can learn in this forum. Sometimes reading in advanced or jumping from one topic to the other could make me forget the basics. But it's better to read about the lessons than creating a program away from the lessons in our class. It's confusing to proceed to a new topic without passing through the basics.

Just this morning, i finished my homework. But the constructors i made could not convert a string into an integer. Maybe there's a formula for it or something.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
...how about <fstream>
« Reply #5 on: February 19, 2006, 11:49:24 AM »
You can use the atoi function from ANSI-C, or istringstream from C++ to do the work.  There is also a Boost library called lexical_cast that will let you do the following:

Code: [Select]
int i = lexical_cast<int>( myString );
Gillius
Gillius's Programming http://www.gillius.org/