473,720 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing help

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 "backupmatr ix"
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.eo f() )
{
employeefile.ge tline (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 "backupmatr ix[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
5 1917
ms******@wnec.e du 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 "backupmatr ix"
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.eo f() )
{
employeefile.ge tline (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 "backupmatr ix[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_N ameGottenFromUs er_
AndParsesTheUse rIDAfterFigurin gOutWhichLineIt Is 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
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.eo f() )
{
employeefile.ge tline (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 "backupmatr ix[linecount]"
}
linecount++;
}
}
}
Hopefully this is a little easier to read. Thanks.

Jun 19 '06 #3

"Sammo" <ms******@wnec. edu> wrote in message
news:11******** *************@c 74g2000cwc.goog legroups.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.eo f() )
{
employeefile.ge tline (backupmatrix[linecount],100);
// using std::vector< std::string > I would use:
// std::string Line;
// employeefile.ge tline( Line );
// backupmatrix.pu sh_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 "backupmatr ix[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
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******** *************@c 74g2000cwc.goog legroups.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.eo f() )
{
employeefile.ge tline (backupmatrix[linecount],100);
// using std::vector< std::string > I would use:
// std::string Line;
// employeefile.ge tline( Line );
// backupmatrix.pu sh_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 "backupmatr ix[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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5060
by: Boris Boutillier | last post by:
Hi all, I'm looking for parsing a Verilog file in my python module, is there already such a tool in python (a module in progress) to help instead of doing a duplicate job. And do you know of some generic parsing module in python, in which you give some kind of grammar and callbacks ? Thanks for the help
16
2900
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
0
4126
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
2
6804
by: John Young | last post by:
I'm trying to parse a directory, but am not sure of the best way of doing it. Preferably using only .net instructions. Can anyone give me an idea of how to do this? Thanks in advance for any help that anyone can give me... John
7
5124
by: Lucas Tam | last post by:
Hi all, Does anyone know of a GOOD example on parsing text with text qualifiers? I am hoping to parse text with variable length delimiters/qualifiers. Also, qualified text could run onto mulitple lines and contain characters like vbcrlf (thus the multiple lines). Anyhow, any help would be appreciated. Thanks!
1
2426
by: yonido | last post by:
hello, my goal is to get patterns out of email files - say "message forwarding" patterns (message forwarded from: xx to: yy subject: zz) now lets say there are tons of these patterns (by gmail, outlook, etc) - and i want to create some rules of how to get them out of the mail's html body. so at first i tried using regular expressions: for example - "any pattern that starts with a <p> and contains "from:"..." etc.
9
4061
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for numerous players whose IDs I have stored in a text file (e.g.: cobbty01, ruthba01, speaktr01, etc.)....
1
1644
by: syhzaidi | last post by:
How can we do Parsing of Hexdecimel in C# reading string from stream file for eg.. i have a file like.......... 0f 2f 12 2d 3a.......in hexa decimal save in a file.txt and i m reading it from the file....... now i have to convert this in decimal and save in an array.of integers.......i thought it can be achieved through parsing ..means 0f could be stored in array converted in decimal...but remmber i m reading from a file///////////////i...
3
4384
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in the file) with the location. And for a particular section I parse only that section. The file is something like, .... DATAS
3
2079
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9365
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7998
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.