C++(Payroll Program)

  • #1
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class payroll
{
private:
char address[50];
float basic,allowence,deduction;
public:
void add(void);
};
void payroll::add(void)
{
clrscr();
int e_no,choice;
char name[50],ch;
payroll e;
cout<<"Enter Employe Number: ";cin>>e_no;
cout<<"Enter Employe Name: ";gets(name);
cout<<"Enter Employe's Address: ";gets(e.address);
cout<<"Enter Employe's Basic Salary: ";cin>>e.basic;
cout<<"Enter Employe's Allownce: ";cin>>e.allowence;
cout<<"Enter Deduction: ";cin>>e.deduction;
cout<<"\n\dfn1-> Save 2-> Cancel";cin>>choice;
if(choice==1)
{
FILE*fptr;
fptr=fopen("d:\\bank.dat","r+");
fwrite(&e,sizeof(e),1,fptr);
fclose(fptr);
}
else if(choice==2)
cout<<"Record Cancelled";
clrscr();
}
payroll emp;
void main(void)
{
clrscr();
char ch;
int choice;
cout<<"\n============main menu============";
cout<<"\n1 -> Add Record";
cout<<"\n2 -> List Record";
cout<<"\n3 -> Delete Record";
cout<<"\n4 -> Update Record";
cout<<"\nq -> Quit\n\n";
cin>>ch;
switch(ch)
{
case'1': emp.add();break;
case'q': break;
}
getch();
}




Here is my code.
I am trying to create a payroll of an employee. As you can see by code that I am yet able to create record just.
But after giving all information when I try to save my record by pressing 1, nothing happens and when I press 2 it should show a statement "Record canceled" but it is not.
 

Answers and Replies

  • #2
560
1
The next line after the cout record cancelled is "clrscr"... Could you possibly be printing this then erasing the screen?

Also you should put a \n at the end of that cout...

As for '1' not working: does bank.dat exist? If you put a cout in the block along with the file operations, does it show that branch is even being executed? What does fopen return? Is errno set after either fopen or fwrite? Don't you need to open the file in binary mode (rb+) to use fwrite in this way?
 
  • #3
Borg
Science Advisor
Gold Member
1,892
2,487
The else if(choice==2) statement does not have brakets after it so only the cout<<"Record Cancelled" command is considered part of that else if. Therefore the clrscr() command is occurring after both choices. If you would indent properly and always use brakets, it would jump right out.
 
  • #4
As for '1' not working: does bank.dat exist? If you put a cout in the block along with the file operations, does it show that branch is even being executed? What does fopen return? Is errno set after either fopen or fwrite? Don't you need to open the file in binary mode (rb+) to use fwrite in this way?

My one problem has been solved. Thanks a lot for that i.e. Record canceled is shown to screen.
But your above statement that I have quoted, I don't understand that.
Can you change my code for what you are saying? Please.
It is a request to you or to any one who could help me. I can't able to open, write and save my file to the HDD(hard disk drive).
 
  • #5
The else if(choice==2) statement does not have brakets after it so only the cout<<"Record Cancelled" command is considered part of that else if. Therefore the clrscr() command is occurring after both choices. If you would indent properly and always use brakets, it would jump right out.

Thanks your information helped me. Can you tell me how to save my file and my file doesn't exist, it will create first but I dunno how to do it. Please.
 
  • #6
Borg
Science Advisor
Gold Member
1,892
2,487
Thanks your information helped me. Can you tell me how to save my file and my file doesn't exist, it will create first but I dunno how to do it. Please.
Sorry, I haven't programmed C++ since I took the course 10 years ago. I can spot simple formatting errors but, I couldn't tell you specifics about the API.
 
  • #7
Sorry, I haven't programmed C++ since I took the course 10 years ago. I can spot simple formatting errors but, I couldn't tell you specifics about the API.

Ok ok. Thanks.
 
  • #8
560
1
My one problem has been solved. Thanks a lot for that i.e. Record canceled is shown to screen.
But your above statement that I have quoted, I don't understand that.
Can you change my code for what you are saying? Please.
It is a request to you or to any one who could help me. I can't able to open, write and save my file to the HDD(hard disk drive).

I haven't tried to run your program, so I don't know what changes need to be made.

However I can give you some advice on debugging it.

The first thing you should do is read the fopen "man page".

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

There are "man pages" for any standard C function.

Things to look at specifically here include:

"r+" Open a file for update both reading and writing. The file must exist.
"w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file.

In other words, fopen with r+ will not work unless the file exists before the code runs.

Also:

If the file has been succesfully opened the function will return a pointer to a FILE object that is used to identify the stream on all further operations involving it. Otherwise, a null pointer is returned.

Checking the return value of fopen() to see if it is NULL or nonzero would help a lot in understanding where the failure is occurring.

Another thing that would help is checking the value of "errno". This is a global variable which is set if fopen() or fwrite() fail; you can check it in a convenient way with the perror() function. However I do not think this exists on Windows.

One more thing to notice from the man page:

With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").

You are not writing text, you are writing a raw binary structure. Therefore you should open the file in binary mode.

By the way, it is confusing to me why you are using this mix of cout and fopen. There are stream objects that work with files also.
 
  • #9
I haven't tried to run your program, so I don't know what changes need to be made.

However I can give you some advice on debugging it.

The first thing you should do is read the fopen "man page".

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

There are "man pages" for any standard C function.

Things to look at specifically here include:



In other words, fopen with r+ will not work unless the file exists before the code runs.

Also:



Checking the return value of fopen() to see if it is NULL or nonzero would help a lot in understanding where the failure is occurring.

Another thing that would help is checking the value of "errno". This is a global variable which is set if fopen() or fwrite() fail; you can check it in a convenient way with the perror() function. However I do not think this exists on Windows.

One more thing to notice from the man page:



You are not writing text, you are writing a raw binary structure. Therefore you should open the file in binary mode.

By the way, it is confusing to me why you are using this mix of cout and fopen. There are stream objects that work with files also.


I appreciate your effort for me. Thanks a lot. But I wanna say you something that I read your given reference but still I am having problem.
Why don't you make a sample of program that could create a text file.
I will be great full to you. Please help me. I have paper on monday.
 

Related Threads on C++(Payroll Program)

  • Last Post
Replies
17
Views
2K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
4
Views
5K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
2
Views
645
  • Last Post
Replies
6
Views
977
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
8
Views
2K
  • Last Post
Replies
5
Views
1K
Top