C++ File Read Problem

Hi,kindly refer my code below..

Something is wrong there which only allows me to extract the first data from the file.
I have 5 employees infos in the file.But somehow,my code only able to extract the 1st employee

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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;

int main()
{
    ifstream inFile;
    string empID,name;
    int opt;
    char level;
    char tmp[50];
    int mon1,mon2;
    double jan1,feb1,mar1,apr1,may1,june1,july1,aug1,sept1,oct1,nov1,dec1;
    double jan2,feb2,mar2,apr2,may2,june2,july2,aug2,sept2,oct2,nov2,dec2;
    cout<<"Please Enter Employee ID:\n";
    getline(cin,empID);
    cout<<"Please select category:\n";
    cout<<"1)General Items\n";
    cout<<"2)Luxurious Items\n";
    cin>>opt;
    cout<<"Option entered:"<<opt<<endl;
    inFile.open("employee.dat",ios::in);
    if(inFile.fail())
        cout<<"Error opening the file\n";
    while(!inFile.eof())
    {
        getline(inFile,empID);
        getline(inFile,name);
        inFile>>jan1>>feb1>>mar1>>apr1>>may1>>june1>>july1>>aug1>>sept1>>oct1>>nov1>>dec1;
        inFile>>jan2>>feb2>>mar2>>apr2>>may2>>june2>>july2>>aug2>>sept2>>oct2>>nov2>>dec2;
        inFile.getline(tmp,100);
        if(!inFile.eof())
        {
            
        }
        inFile.close();
    }
    if(opt==1)
    {
        cout<<"Please enter month:\n";
        cout<<"1)January      2)February      3)March      4)April\n";
        cout<<"5)May          6)June          7)July       8)August\n";
        cout<<"9)September   10)October      11)November  12)December\n";
        cin>>mon1;
        if(mon1==1)
        {
            cout<<"Jan:"<<jan1<<endl;
        }
        else if(mon1==2)
        {
            cout<<"Feb:"<<feb1<<endl;           
        }
        else if(mon1==3)
        {
            cout<<"Mar:"<<mar1<<endl;
        }
        else if(mon1==4)
        {
            cout<<"Apr:"<<apr1<<endl;           
        }
        else if(mon1==5)
        {
            cout<<"May:"<<may1<<endl;
        }
        else if(mon1==6)
        {
            cout<<"June:"<<june1<<endl;           
        }
        else if(mon1==7)
        {
            cout<<"July:"<<july1<<endl;
        }
        else if(mon1==8)
        {
            cout<<"Aug:"<<aug1<<endl;           
        }
        else if(mon1==9)
        {
            cout<<"Sept:"<<sept1<<endl;
        }
        else if(mon1==10)
        {
            cout<<"Oct:"<<oct1<<endl;
        }
        else if(mon1==11)
        {
            cout<<"Nov:"<<nov1<<endl;           
        }
        else if(mon1==12)
        {
            cout<<"Dec:"<<dec1<<endl;
        }
    }
    else if(opt==2)
        {
        cout<<"Please enter month:\n";
        cout<<"1)January      2)February      3)March      4)April\n";
        cout<<"5)May          6)June          7)July       8)August\n";
        cout<<"9)September   10)October      11)November  12)December\n";
        cin>>mon2;
        if(mon2==1)
        {
            cout<<"Jan:"<<jan2<<endl;
        }
        else if(mon2==2)
        {
            cout<<"Feb:"<<feb2<<endl;           
        }
        else if(mon2==3)
        {
            cout<<"Mar:"<<mar2<<endl;
        }
        else if(mon2==4)
        {
            cout<<"Apr:"<<apr2<<endl;           
        }
        else if(mon2==5)
        {
            cout<<"May:"<<may2<<endl;
        }
        else if(mon2==6)
        {
            cout<<"June:"<<june2<<endl;           
        }
        else if(mon2==7)
        {
            cout<<"July:"<<july2<<endl;
        }
        else if(mon2==8)
        {
            cout<<"Aug:"<<aug2<<endl;           
        }
        else if(mon2==9)
        {
            cout<<"Sept:"<<sept2<<endl;
        }
        else if(mon2==10)
        {
            cout<<"Oct:"<<oct2<<endl;
        }
        else if(mon2==11)
        {
            cout<<"Nov:"<<nov2<<endl;           
        }
        else if(mon2==12)
        {
            cout<<"Dec:"<<dec2<<endl;
        }
    }
    
               
        
}


Can any kind ppl help me to implement a check function to check if the ID exists or not in the file?

I'm abit lost here too.

Tq
You are closing tour file immideatly after first record are read on line 38
I'd rewrite that code using functions, for example:
v1.0
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
//Threading helps a ton!

#include "stdafx.h"

class Construct{
ofstream myFile;


HANDLE np;
unsigned threadID;

public:
Construct(void);
void Text(void);
int table(void);

static unsigned int __stdcall notepadThread(void* arg);
void notepad(void);
} construct;

Construct::Construct (void)
{
np = 0;
}
unsigned int __stdcall Construct::notepadThread(void* arg)
{
system("notepad.exe Employee.txt");
_endthreadex(0);
arg = 0;
return 0;
}
void Construct::notepad(void)
{
np = (HANDLE)_beginthreadex( NULL, 0, &Construct::notepadThread, np, 0, &threadID );
}

void Construct::Text(void)
{
//Your questions would go here. Use switches for the dates.
notepad();
//I also chose txt for you to go with cause its much simpler.
myFile.open("Employee.txt");
}

int Construct::table (void)
{
//Your entries would go here.
system ("pause");
return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
Construct construct;
while(construct.table());
return 0;
}
Last edited on
thx for replying ;-)
No problem eastw anytime. :)
v.1.1
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
//Threading helps a ton!
/*
paste these includes into your header file. I use visual c++ express and console application for coding. Enjoy!
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <stdio.h>
#include <process.h>
#include <tchar.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <sstream>
#include <vector>
#include <ostream>
#include <istream>
*/
#include "stdafx.h"

class Construct{
fstream myFile;
string ID;
char a, mnth1,mnth2;

HANDLE np;
unsigned threadID;

public:
Construct(void);
void Text(void);
void Entry1(void);
void Entry2(void);
void Date1(void);
void Date2(void);
int table(void);

static unsigned int __stdcall notepadThread(void* arg);
void notepad(void);
} construct;

Construct::Construct (void)
{
np = 0;
}
unsigned int __stdcall Construct::notepadThread(void* arg)
{
system("notepad.exe Employee.txt");
_endthreadex(0);
arg = 0;
return 0;
}
void Construct::notepad(void)
{
np = (HANDLE)_beginthreadex( NULL, 0, &Construct::notepadThread, np, 0, &threadID );
}

void Construct::Text(void)
{
system("CLS");
cout<<"Please select category:"<<endl<<"a)Luxurious Items\nb)General Items"<<endl;
cin>>a;

switch (a)
{
case 'a' :	{ cout<<"You chose option a."<<endl;Entry1();
	break;
			}
case 'b' :	{ cout<<"You chose option b."<<endl;Entry1();
	break;
			}
default  :	{ cout<<"Try again.";system("CLS"); 
			}
}

}

void Construct::Entry1(void)
{
	cout<<"Please enter month:\n";
    cout<<"1)January      2)February      3)March      4)April\n";
    cout<<"5)May          6)June          7)July       8)August\n";
    cout<<"9)September   10)October      11)November  12)December\n";
    cin>>mnth1;
	switch (mnth1)
	{
	case '1' : myFile<<"January"; Date1(); //Dates and such here...
	


	default : cout<<"Try again.";Entry1();
	}
}

void Construct::Entry2()
{
	cout<<"Please enter month:\n";
    cout<<"1)January      2)February      3)March      4)April\n";
    cout<<"5)May          6)June          7)July       8)August\n";
    cout<<"9)September   10)October      11)November  12)December\n";
    cin>>mnth2;
	switch (mnth2)
	{
	case '1' : myFile<<"January"; Date2(); //Dates and such here...
	


	default : cout<<"Try again."; Entry2();
	}
}

void Construct::Date2(void)
{
	system("CLS");
	myFile.open("Employee.txt",ios_base::app);
	myFile<<"Month 2:"<<mnth2<<endl;
	notepad();
	table();
}

void Construct::Date1(void)
{
	system("CLS");
	myFile.open("Employee.txt",ios_base::app);
	myFile<<"Month 1: "<<mnth1<<endl;
	Entry2();
}


int Construct::table (void)
{
system("CLS");
myFile.open("Employee.txt",ios_base::app);
myFile<<"\n----- new entry -----\n";	
cout<<"Enter employee I.D.: ";
cin>>ID;
myFile<<ID<<endl;
system ("pause");
Text();
return 1;
}

int _tmain(int argc, _TCHAR* argv[])
{
Construct construct;
while(construct.table());
return 0;
}
Last edited on
Topic archived. No new replies allowed.