C++ Payroll Program Troubleshooting

  • Context: Comp Sci 
  • Thread starter Thread starter shayaan_musta
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a C++ payroll program that is intended to create and save employee records. Participants are addressing issues related to file handling, user input, and code structure, focusing on debugging and improving the program's functionality.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant notes that the screen may be cleared immediately after displaying the "Record Cancelled" message, suggesting that the order of operations could be affecting visibility.
  • Another participant questions whether the file "bank.dat" exists and suggests checking the return value of fopen to determine if the file operations are being executed correctly.
  • There is a suggestion to open the file in binary mode, as fwrite may require it for proper functionality.
  • Concerns are raised about the lack of brackets in the else if statement, which could lead to unintended execution of commands.
  • Some participants express uncertainty about how to create a file if it does not already exist and request specific code changes to address this issue.
  • One participant mentions they have not programmed in C++ for a long time and can only identify simple formatting errors, indicating a need for more detailed guidance.
  • Another participant offers advice on debugging, including checking the value of errno and understanding the fopen man page, but expresses uncertainty about specific changes needed in the code.
  • There is a request for a sample program that could create a text file, indicating a desire for practical examples to aid understanding.

Areas of Agreement / Disagreement

Participants generally agree on the need for debugging and improving the code, but multiple competing views and uncertainties remain regarding file handling and program structure. The discussion does not reach a consensus on specific solutions.

Contextual Notes

Participants mention limitations related to the existing code structure, such as the need for proper use of brackets and the requirement for binary file handling. There is also a lack of clarity on how to create files if they do not exist, which remains unresolved.

Who May Find This Useful

This discussion may be useful for individuals learning C++ programming, particularly those interested in file handling and debugging techniques in the context of payroll or record-keeping applications.

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;
count<<"Enter Employe Number: ";cin>>e_no;
count<<"Enter Employe Name: ";gets(name);
count<<"Enter Employe's Address: ";gets(e.address);
count<<"Enter Employe's Basic Salary: ";cin>>e.basic;
count<<"Enter Employe's Allownce: ";cin>>e.allowence;
count<<"Enter Deduction: ";cin>>e.deduction;
count<<"\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)
count<<"Record Cancelled";
clrscr();
}
payroll emp;
void main(void)
{
clrscr();
char ch;
int choice;
count<<"\n============main menu============";
count<<"\n1 -> Add Record";
count<<"\n2 -> List Record";
count<<"\n3 -> Delete Record";
count<<"\n4 -> Update Record";
count<<"\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 count 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 count...

As for '1' not working: does bank.dat exist? If you put a count 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 count<<"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 count 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 count<<"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 count 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 count 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 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K