c++ file handling program errors

Here's the program
Its a simple program that asks for a name and saves it in a dat file and can display it.
<code>
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<ctype.h>

using namespace std;

class struct{
private: char *empname
float bsal;
public: int getdata();
int write();
int display();
};
struct obj;
int struct::getdata()
{
cout<<"Enter employee Name"<<endl;
gets(obj.empname);
cout<<"Enter Basic Salary"<<endl;
cin>>bsal;
return 0;

}
int struct::write()
{
ofstream fout;
fout.open("database.dat",ios::out|ios::binary|ios::app)
if(!fout)
{
cout<<"File can not be opened"<<endl;
return 0;
}
else
{
char ch;
do{
cout<<"Enter Employee Details"<<endl;
obj.getdata();
fout.write((char*)&obj,sizeof(obj));
cout<<"Do you want to enter another record:"<<endl;
cin>>ch;
}
while(ch=='y'||ch=='Y')
}
fout.close();
return 0;
}
int struct::display()
{
ifstream fin;
fin.open("database.dat",ios::in|ios::binary);
if(!fin)
{
cout<<"The file can not be opened";
return 0;
}
else
{
while(fin)
{
fin.read((char*)&obj,sizeof(struct));

puts(obj.empname);


}
}fin.close();
return 0;
}
int main(void)
{
stuct s;
s.getdata();
s.write();
s.display();

}

}</code>
I think I am getting mixed up with c++ standard syntaxes
I use dev c++ 5.5.3
errors are
line col message
8 7 expected identifier before 'struct'
14 13 multiple types in one declaration
16 13 'getdata' in namespace '::' does not name a type
16 21 expected unqualified-id before ')' token
25 13 'write' in namespace '::' does not name a type
25 19 expected unqualified-id before ')' token

I suggest you start by formmating your code.

I'll point out a couple things I see that appears wrong

1
2
while(ch=='y'||ch=='Y')	
}


1
2
3
4
5
6
7
8
int main(void)
{
stuct s;
s.getdata();
s.write();
s.display();
}
}


I suggest that when you do that you'll find a few more.
I found another error that i cant keep a c++ keyword struct as a class name. Wait I am going to re post a simple version of this code so that you guys can check it.
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
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<ctype.h>

using namespace std;

struct abc{
	         char *empname;
	         float bsal;
          }obj[6];
            
int getdata(void)
{
	cout<<"Enter employee Name and salary"<<endl;
	for(int i=0; i<6 ; i++)
	{
	
	gets(obj[i].empname);
	cin>>obj[i].bsal;
    } 
	
}
int write(void)
{
	ofstream fout;
	fout.open("database.dat",ios::out|ios::binary|ios::app);
	if(!fout)
	{
		cout<<"File can not be opened"<<endl;
		
	}
	else
	{

		cout<<"Enter Employee Details"<<endl;
		getdata();
		fout.write((char*)&obj,sizeof(abc)); 
	}
	fout.close();
}
int display(void)
{
	ifstream fin;
	fin.open("database.dat",ios::in|ios::binary);
	if(!fin)
	{
		cout<<"The file can not be opened";
		return 0;
	}
	else
	{
		while(fin)
		{
			fin.read((char*)&obj,sizeof(abc));
			    for(int i=0; i<6 ; i++)
			    {
				puts(obj[i].empname);
				cin>>obj[i].bsal;
			    }
	
			
		}
	}fin.close();

} 
int main()
{
	
	getdata();
	write();
	display();
	return 0;
	
}
See I have completely remade the code. Can you guys check if it is logically correct please. I will be very grateful.
It compiled successfully but output is absurd
Last edited on
The code you posted above does not compile:


main.cpp(23): error C4716: 'getdata' : must return a value
main.cpp(66): warning C4715: 'display' : not all control paths return a value
main.cpp(41): error C4716: 'write' : must return a value

Well guess i figured out the proper code by my self. Here it is
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
#include<iostream>
#include<fstream>
#include<cstdio>

using namespace std;

int n;

struct employee
{
	int empno;
	char name[25];
	int wage;
}emp;

void append()
{
	ofstream fout;
	fout.open("database.txt",ios::out);
	cout<<"No of employees"<<endl;
	cin>>n;
	for(int i=0; i<n ; i++)
	{
		cout<<"Enter emp ID"<<endl;
		cin>>emp.empno;
		cout<<"Enter name"<<endl;
		cin.get();
		cin.getline(emp.name, 20);
		cout<<"Enter wage/hr"<<endl;
		cin>>emp.wage;
		fout.write((char*)&emp,sizeof(emp));
		
	}
	fout.close();

}

void display ()
{
	ifstream fin;
	fin.open("database.txt",ios::in);
	cout<<"Employee Data base"<<endl;
	while(fin)
	{
	    fin.read((char*)&emp,sizeof(emp));
		cout<<emp.empno<<"   "; puts(emp.name);  cout<<"  "<<emp.wage<<endl;

	}
	fin.close();
}
int main()
{
	
	append();
	display();
	return 0;
}

It compiles and runs on DEV c++ version 5.11
Topic archived. No new replies allowed.