Comp Sci C++ Payroll Program Troubleshooting

  • Thread starter Thread starter shayaan_musta
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around troubleshooting a C++ payroll program where the user is unable to save employee records to a file. Key issues identified include the improper use of file opening modes, specifically needing to open files in binary mode for writing, and the lack of brackets in conditional statements that lead to unintended behavior in the code execution. Suggestions are made to check the return value of the fopen function and to use the perror function to diagnose errors related to file operations. The user also requests assistance in creating a sample program that can successfully create and write to a text file. Overall, the conversation highlights common pitfalls in file handling in C++.
shayaan_musta
Messages
208
Reaction score
2
#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.
 
Physics news on Phys.org
The next line after the cout record canceled 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?
 
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.
 
Coin said:
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 anyone who could help me. I can't able to open, write and save my file to the HDD(hard disk drive).
 
Borg said:
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 don't know how to do it. Please.
 
shayaan_musta said:
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 don't know 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.
 
Borg said:
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.
 
shayaan_musta said:
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 anyone 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.
 
Coin said:
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 want to 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.
 

Similar threads

Replies
3
Views
1K
Replies
7
Views
2K
Replies
3
Views
1K
Replies
14
Views
4K
Replies
13
Views
3K
Replies
6
Views
3K
Back
Top