FileHandling in Class

How can you run fstream in Class?
Can you tell me what wrong with my code, my code is about file handling more of like a attendance in a company. I have a Employee and a Admin. Employee can only put there name or id number then the code will just automatically put it in the text file and then the admin will be secured with a password and will have the power to see the text file and he/she can remove or add employee.
Here is what my code look likes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <windows.h>
#include <fstream>
#include <ctime>

using namespace std;

class company {
private : string username;
string password;
int tries;
int ch;


public :

company()
{
username = " ";
password = " ";
tries = 1;
}
void login()
{
wrong:
cout<<"\t User Name: ";
cin>>username;
cout<<"\t Password: " ;
while(tries)
{
while(ch=getch())
{
if(ch == 13)//check ch after press RETURN key;
{
if ( username=="Admin"&&password=="admin1234")
{
cout<<"\t Hello " <<username <<endl;
break;
}
//system("cls");
cout<<"\n Wrong User Name or Password.\n";
password = "";
tries += 1;
if(tries == 4)
{
cout<<"You've Tried 3 times. Exiting.\n";
}
goto wrong;
}
else
if(ch==8)//check ch after press BACKSPACE key;
{
if(password.length()>0)//set condition blocking error while input
{
cout<<"\b \b";//remove Mask * on screen;
password.erase(password.length()-1); //erase String length
}
}
else
{
cout<<"*";
password += ch; //the input password was assigned to variable passwd.
}
}
}
}

int employee()
{
time_t t= time(0);
struct tm *now = localtime(&t);
ofstream fout; // Creation of ofstream class object
string line;


fout.open("output.txt", ios::app);   // by default ios::out mode, automatically deletes
// the content of file. To append the content, open in ios:app
// fout.open("sample.txt", ios::app)


while (fout)  // Execute a loop If file successfully opened
{
cout<<"TYPE : ";
// Read a Line from standard input
getline(cin, line);

// Press 1 to exit
if (line == "1")
break;

// Write line in file
fout << line << " " << (now->tm_year +1900) <<'-'<< (now->tm_mon+1) << '-'<<now->tm_mday
<<" "<<now->tm_hour<< ':'<<now->tm_min << ':'<<now->tm_sec<< endl;
}

// Close the File
fout.close();

// Creation of ifstream class object to read the file
ifstream fin;

// by default open mode = ios::in mode
fin.open("output.txt");

// Execute a loop until EOF (End of File)
while (fin) {

// Read a Line from File
getline(fin, line);

// Print line in Console
cout << line << endl;
}

// Close the file
fin.close();
}

void example()
{
cout<<"BOBO PUTANGINAMO \n";
}


};
int main ()
{
company list;
int choice;
do
{
cout<<"1. Admin.\n";
cout<<"2. Employee.\n";
cout<<"3.Exit.\n";
cin>>choice;
cout<<endl;
}while(choice!=3);

switch(choice)
{
case 1 : list.login();
break;
case 2 : list.employee();
break;
case 3 : cout<<"BYE!.\n";
break;
case 4 : list.example();
break;


}


system("pause");
return 0;

}
Last edited on
Hello wannabeako,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

Just to let you know "conio.h" is not a standard C++ header file. Actually it is out dated and no longer used or supported.

Since you did not ask a real question it will take a little time to load up the program and see what it is doing.

Andy
thank you very much im new to this.
A crucial part of Andy's post was:

Since you did not ask a real question


Perhaps you could rectify that? What, specifically, is your problem? What, specifically, are you having trouble with?
My problem is that how can you run Fstream/file handling in Class.
Hello wannabeako,

After working with the program a bit the first thing I found is you defined two "std::strings", but did not include the header file "string". This does cause a problem.

using namespace std;. This is best not to use.

Best to avoid using "goto" statements. This can be replaced with a do/while loop.

I have not fully tested the while loop in "userlogin", but it appears to work.

In the function "time", which I have not tested yet, I do have a concern about the while loops. "while (fout)" is not the best condition for a loop like this. For the output loop the while condition of 1 or true works better than basing it on the condition of the file stream. Then inside the loop you need to let the user know what to enter to when finished.

When you open a file for input the first thing you should is check that the file stream is open. A good idea for the output file, but not as necessary because the output will create the file if it does not exist.

What I like to use is:
1
2
3
4
5
6
if (!fin)
{
    std::cout << "\n An error message \"" << fileName << "\" did not open" << std::endl; 
    std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
    return 1;
}

The variable "fileName" would be defined as a "std::string" that could be used in more than one place. Or you could just use the file name and put it in double quotes.

The second line just caused the program to pause. The only things you need to do is include the header files, as noted, and change the "3" to whatever you need. The "3" refers to the number of seconds it will pause.

The "1" in "return 1;" will let you know that there is a problem. This can be any number, other than zero, that you want. Or you could use "exit(1);" to leave the program instead of returning to "main".

For reading it is more common to use while (std::getline(fin, line)). This way when you try to read past end of file the condition and while loop will fail.

Thought for the future. Instead of just reading the file and displaying the results consider doing something to add the information to a vector or an array for later use.

In "main" the code is correct, but the switch should be inside the do/while loop not after it. As it is only choice "3" works. I also noticed the the switch has four case statements, but the menu options only have three.

Hope that helps,

Andy
Hello wannabeako,

My problem is that how can you run Fstream/file handling in Class.


What class? I do not see any in your OP code.

Any way in a member function of a class it would be the same as you have already done.

Or you can set up and open the file, say in "main", and pass the file stream to the function.

Hope that helps,

Andy
Topic archived. No new replies allowed.