By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Parsing help

P: n/a
Hey, i have a payroll system and i'm tyring to have it so the user can
select an option from the menu and then be able to change one of the
employee's salaries based on the user ID input. ( data is read in from
a text file.)

If (choice == 2)
{
cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
cin >> EmpID;
cout << " " << endl;
char buffer[20];
{
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20];

char backupmatrix[100][100]; //to take full file into "backupmatrix"
ifstream employeefile ("employee.txt"); //read from file

if (! employeefile.is_open())
{ cout << "Error opening file"; exit (1); }

int linecount = 0; //separates lines in file when taken out
int IDlength = strlen(EmpID); //gets length of ID

while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100); //gets single line
cout << backupmatrix[linecount] << endl; //output for testing purposes

for( int counter = 0; counter < IDlength; counter++) //checks
employee's ID to inputted ID
{
if( backupmatrix[linecount][counter+2] != EmpID[counter]
) {break;} //if don't match, leave for loop

if( counter == (IDlength-1) )//if full ID matches, change salary

{cout << "HEY" << endl;} //for debugging purposes, replace cout with
means of
/changing SALARY portion of the line "backupmatrix[linecount]"
}
linecount++;
}
}
}
}
}

So far when its ran and a correct employee ID is input {cout << "HEY"
<< endl;} does output like intended. This is just a section of the
code. Any suggestions or changes are greatly appreciated. Thanks!

Jun 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ms******@wnec.edu wrote:
Hey, i have a payroll system and i'm tyring to have it so the user can
select an option from the menu and then be able to change one of the
employee's salaries based on the user ID input. ( data is read in from
a text file.)
Ok, I'm not sure I see any specific questions in your post.
Where there any? I can give a generic specific suggestions.

One is, possibly due to the strange nature of the posting
process, your code was strangely formatted with line breaks
and indents that made it somewhat hard to read. Try to pick
a format style that makes things easy to read.
If (choice == 2)
{
cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
cin >> EmpID;
cout << " " << endl;
char buffer[20];
{
Here is an example of the weird formatting. I'm having a hell of a
time figuring out the { bracket there. What is it opening?

The 2 in the choice == 2 test is a magic number. Think about
using const values instead.

Think about dropping the C-style char array and using std::string
instead.
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20];

char backupmatrix[100][100]; //to take full file into "backupmatrix"
ifstream employeefile ("employee.txt"); //read from file
More weird formatting. The indent seems to have magically
dropped back to the left edge.
if (! employeefile.is_open())
{ cout << "Error opening file"; exit (1); }
My personally bias is: One line of code, one line of file.
Extra lines are not expensive. So I would format this
something like so.

if (! employeefile.is_open())
{
cout << "Error opening file";
exit (1);
}

Also: Using exit here *may* be appropriate, but maybe not. Think
about making your routines such that error conditions that fit in
the defined interface are carried through the interface, and those
that don't fit in the interface are carried through the exception
mechanism.

Think about the situation where your code becomes widely used.
And then somebody wants to adapt it to a library. You probably
don't want "exit(1)" in a library, as you probably don't want the
library aborting program execution.

You didn't show what this code snippet fits in, so it's not really
possible to say what the best way to deal with this is.
int linecount = 0; //separates lines in file when taken out
int IDlength = strlen(EmpID); //gets length of ID

while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100); //gets single line
cout << backupmatrix[linecount] << endl; //output for testing purposes

for( int counter = 0; counter < IDlength; counter++) //checks
employee's ID to inputted ID
{
if( backupmatrix[linecount][counter+2] != EmpID[counter]
) {break;} //if don't match, leave for loop
More confusing indents. The 2 there appears to be a magic number.
if( counter == (IDlength-1) )//if full ID matches, change salary

{cout << "HEY" << endl;} //for debugging purposes, replace cout with
means of
/changing SALARY portion of the line "backupmatrix[linecount]"
}
linecount++;
}
}
}
}
}
You've got six deep brackets here. This is probably too many.

Try to write short single purpose routines. You can tell you are
doing a good job at this if you can accurately name your routines
in the <verb><noun> pattern. OpenFile, or ParseID, or like that.
If you have a routine that OpensAnIDFile_NameGottenFromUser_
AndParsesTheUserIDAfterFiguringOutWhichLineItIs then you
see the problem right away.
So far when its ran and a correct employee ID is input {cout << "HEY"
<< endl;} does output like intended. This is just a section of the
code. Any suggestions or changes are greatly appreciated. Thanks!


Here are a couple books to get.

_Accelerated C++_ by Koenig and Moo
_Code Complete_ by Steve McConnell

Socks

Jun 19 '06 #2

P: n/a
Hey, sorry about the messy format let me know if this is any better.
My question is how do i get it so the user is able to input a new
salary so that it will replace the old on within the text file. As for
the (choice == 2) that comes from a menu where the user is able to pick
1 of 2 choices. The menu works fine, i'm just having a trouble
implementing this last option. Also i realize there is 6 brackets at
the end. Some of them aren't for the snippet i posted, they go with
the rest of the program.

if (choice == 2)
{

cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
cin >> EmpID;
cout << " " << endl;
char buffer[20];
{
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20];
char backupmatrix[100][100];
ifstream employeefile ("employee.txt");

if (! employeefile.is_open())
{ cout << "Error opening file"; exit (1); }

int linecount = 0;
int IDlength = strlen(EmpID);

while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100);
cout << backupmatrix[linecount] << endl;

for( int counter = 0; counter < IDlength;
counter++) {
if( backupmatrix[linecount][counter+2] != EmpID[counter] ) {break;}

if( counter == (IDlength-1) )
{cout << "TEST!!!!!!!!!" << endl;} //for debugging purposes
line "backupmatrix[linecount]"
}
linecount++;
}
}
}
Hopefully this is a little easier to read. Thanks.

Jun 19 '06 #3

P: n/a

"Sammo" <ms******@wnec.edu> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Hey, sorry about the messy format let me know if this is any better.
My question is how do i get it so the user is able to input a new
salary so that it will replace the old on within the text file. As for
the (choice == 2) that comes from a menu where the user is able to pick
1 of 2 choices. The menu works fine, i'm just having a trouble
implementing this last option. Also i realize there is 6 brackets at
the end. Some of them aren't for the snippet i posted, they go with
the rest of the program.


Here, I've formatted it a bit better. A few comments in the code marked
with //

if (choice == 2)
{

cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
// EmpID should be std::string
cin >> EmpID;
cout << " " << endl;
// Buffer shoudl be
char buffer[20]; // What is buffer for? Your're not using it.
{ // Why is this here? No reason to block the following code
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20]; // Again, you're not using this var

char backupmatrix[100][100];
// Better here would be std::vector< std::string > backupmatrix;
ifstream employeefile ("employee.txt");

if (! employeefile.is_open())
{
cout << "Error opening file";
exit (1); // Change this to return instead of exit.
}

int linecount = 0;
int IDlength = strlen(EmpID);

while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100);
// using std::vector< std::string > I would use:
// std::string Line;
// employeefile.getline( Line );
// backupmatrix.push_back( Line );

cout << backupmatrix[linecount] << endl;
// That line would work without changes.

// with std::string you can use string.compare()
for( int counter = 0; counter < IDlength; counter++)
{
if ( backupmatrix[linecount][counter+2] != EmpID[counter] )
{
break;
}

if ( counter == (IDlength-1) )
{
cout << "TEST!!!!!!!!!" << endl;
} //for debugging purposes
line "backupmatrix[linecount]"
}
linecount++;
}
}
}

Okay. Here's the situation. You have the employee's names and salaries in
a text file, correct? And you want to update this file. Well, the thing
with text files is, you dont usually just get to update them in place,
because they are variable width. There are normally two ways to deal with
this.

1. Read the entire file into a buffer. Make changes into the buffer. Write
the new file to a new name. Copy over your new file to the old one.

or

2. Read the file line by line. If the line is not effected, write it to a
new file. If it is effected, write the changes to the new file. Continue
til end of file. Copy over your new file to the old one.

Each method has it's advantages. The advantage of 1. is it's easier to
determine where to do an insert as you have the entire file in memory. The
disadvantage is for large files it can take quite a huge buffer. The
advantage to 2 is you only have to buffer one line at a time.

Now. As for you reading the lines into a char[][], this can have problems.
What happens if a line is over 100 characters? What if you have more than
100 employees? I would rather use a vector of std::string. Both are
dynamic.
Jun 20 '06 #4

P: n/a
When i was given this task we were given the employee file already.
Knowing there wasn't going to be over 100 employees thats how i decided
to do it at the time. And to answer your questions , yes i have the
employees and salaries within a text file. I have one text file
(employee.txt) which contains all the information about each employee
including Employee ID, name, ssn, salary, start date ...etc. each
seperated by a comma. The user will input an employee ID and based on
that i want to be able to search the file for that employee's salary
and change it with the new salary that the user input. How would do
that using option 1 that you had listed Jim? Thanks


Jim Langston wrote:
"Sammo" <ms******@wnec.edu> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Hey, sorry about the messy format let me know if this is any better.
My question is how do i get it so the user is able to input a new
salary so that it will replace the old on within the text file. As for
the (choice == 2) that comes from a menu where the user is able to pick
1 of 2 choices. The menu works fine, i'm just having a trouble
implementing this last option. Also i realize there is 6 brackets at
the end. Some of them aren't for the snippet i posted, they go with
the rest of the program.


Here, I've formatted it a bit better. A few comments in the code marked
with //

if (choice == 2)
{

cout << "Please Enter the Employee's ID Number (ex. Test0001 <case
sensitive>): " << endl;
// EmpID should be std::string
cin >> EmpID;
cout << " " << endl;
// Buffer shoudl be
char buffer[20]; // What is buffer for? Your're not using it.
{ // Why is this here? No reason to block the following code
cout << "Please Enter a New Salary for the Employee. " << endl;
cout << "$";
cin >> Newpay;
cout << " " << endl;
char buffer[20]; // Again, you're not using this var

char backupmatrix[100][100];
// Better here would be std::vector< std::string > backupmatrix;
ifstream employeefile ("employee.txt");

if (! employeefile.is_open())
{
cout << "Error opening file";
exit (1); // Change this to return instead of exit.
}

int linecount = 0;
int IDlength = strlen(EmpID);

while (! employeefile.eof() )
{
employeefile.getline (backupmatrix[linecount],100);
// using std::vector< std::string > I would use:
// std::string Line;
// employeefile.getline( Line );
// backupmatrix.push_back( Line );

cout << backupmatrix[linecount] << endl;
// That line would work without changes.

// with std::string you can use string.compare()
for( int counter = 0; counter < IDlength; counter++)
{
if ( backupmatrix[linecount][counter+2] != EmpID[counter] )
{
break;
}

if ( counter == (IDlength-1) )
{
cout << "TEST!!!!!!!!!" << endl;
} //for debugging purposes
line "backupmatrix[linecount]"
}
linecount++;
}
}
}

Okay. Here's the situation. You have the employee's names and salaries in
a text file, correct? And you want to update this file. Well, the thing
with text files is, you dont usually just get to update them in place,
because they are variable width. There are normally two ways to deal with
this.

1. Read the entire file into a buffer. Make changes into the buffer. Write
the new file to a new name. Copy over your new file to the old one.

or

2. Read the file line by line. If the line is not effected, write it to a
new file. If it is effected, write the changes to the new file. Continue
til end of file. Copy over your new file to the old one.

Each method has it's advantages. The advantage of 1. is it's easier to
determine where to do an insert as you have the entire file in memory. The
disadvantage is for large files it can take quite a huge buffer. The
advantage to 2 is you only have to buffer one line at a time.

Now. As for you reading the lines into a char[][], this can have problems.
What happens if a line is over 100 characters? What if you have more than
100 employees? I would rather use a vector of std::string. Both are
dynamic.


Jun 20 '06 #5

P: n/a
ok i got that working kinda...but what i need to know now is about
strtok.

say for example i have data in a file in this format

EmpType, EmpID, Name, SSN, Start Date, Salary, Hours

and i want to be able to read and over write the salary input based on
the EmpID.
As of now i have the user inputting the EmpID and it will over write
the salary as long as the salary is in the code itself. I want it to
change whatever is in the placeholder not that specific number. I know
it involves using strtok, but im not really sure on how to do it. Not
sure if this is clear or not so please let me know. Thanks

Jun 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.